Discussion:
Check cursor in procedure
(too old to reply)
Oliver Scheiber
2007-08-21 10:44:08 UTC
Permalink
Hi,
i have developed an AddIn for the VB-IDE using visual basic 6.0.

Now I would like to insert a comment, but only even if the cursor is
in a procedure.

I tried this code but it doesn't works fine, it always comes MsgBox
"Cursor not in procedure!":

Public Function InsertComment()
Dim MyCodePane As CodePane
Dim MyCodeMod As CodeModule
Dim iCurrentLine As Long, b As Long, c As Long, d As Long

Set MyCodePane = VBInstance.ActiveCodePane
Set MyCodeMod = MyCodePane.CodeModule

MyCodePane.GetSelection iCurrentLine, b, c, d
If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MsgBox "Jeah, cursor in procedure!"
MyCodeMod.InsertLines iCurrentLine, "'This is a comment"
End If
End Function

Any suggestion?

Regards Oliver
Jan Hyde (VB MVP)
2007-08-21 11:42:30 UTC
Permalink
Oliver Scheiber <***@gmx.de>'s wild thoughts
were released on Tue, 21 Aug 2007 10:44:08 -0000 bearing the
Post by Oliver Scheiber
Hi,
i have developed an AddIn for the VB-IDE using visual basic 6.0.
Now I would like to insert a comment, but only even if the cursor is
in a procedure.
I tried this code but it doesn't works fine, it always comes MsgBox
Public Function InsertComment()
Dim MyCodePane As CodePane
Dim MyCodeMod As CodeModule
Dim iCurrentLine As Long, b As Long, c As Long, d As Long
Set MyCodePane = VBInstance.ActiveCodePane
Set MyCodeMod = MyCodePane.CodeModule
MyCodePane.GetSelection iCurrentLine, b, c, d
If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MsgBox "Jeah, cursor in procedure!"
MyCodeMod.InsertLines iCurrentLine, "'This is a comment"
End If
End Function
Any suggestion?
If you goal is to insert comments or even code, then you
could save a lot of time by download the free addin MZtools.



--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Oliver Scheiber
2007-08-21 12:40:09 UTC
Permalink
On 21 Aug., 13:42, "Jan Hyde (VB MVP)"
Post by Jan Hyde (VB MVP)
were released on Tue, 21 Aug 2007 10:44:08 -0000 bearing the
Post by Oliver Scheiber
Hi,
i have developed an AddIn for the VB-IDE using visual basic 6.0.
Now I would like to insert a comment, but only even if the cursor is
in a procedure.
I tried this code but it doesn't works fine, it always comes MsgBox
Public Function InsertComment()
Dim MyCodePane As CodePane
Dim MyCodeMod As CodeModule
Dim iCurrentLine As Long, b As Long, c As Long, d As Long
Set MyCodePane = VBInstance.ActiveCodePane
Set MyCodeMod = MyCodePane.CodeModule
MyCodePane.GetSelection iCurrentLine, b, c, d
If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MsgBox "Jeah, cursor in procedure!"
MyCodeMod.InsertLines iCurrentLine, "'This is a comment"
End If
End Function
Any suggestion?
If you goal is to insert comments or even code, then you
could save a lot of time by download the free addin MZtools.
--
Jan Hyde
https://mvp.support.microsoft.com/profile/Jan.Hyde- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
Hi Jan,
I know the MZTools. My code-procedure was only a example. In the right
procedure i would like insert a variable.

If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MyCodeMod.InsertLines iCurrentLine, strText ' < Variable
End If
Jan Hyde (VB MVP)
2007-08-21 13:43:48 UTC
Permalink
Oliver Scheiber <***@gmx.de>'s wild thoughts
were released on Tue, 21 Aug 2007 12:40:09 -0000 bearing the
Post by Oliver Scheiber
On 21 Aug., 13:42, "Jan Hyde (VB MVP)"
Post by Jan Hyde (VB MVP)
were released on Tue, 21 Aug 2007 10:44:08 -0000 bearing the
Post by Oliver Scheiber
Hi,
i have developed an AddIn for the VB-IDE using visual basic 6.0.
Now I would like to insert a comment, but only even if the cursor is
in a procedure.
I tried this code but it doesn't works fine, it always comes MsgBox
Public Function InsertComment()
Dim MyCodePane As CodePane
Dim MyCodeMod As CodeModule
Dim iCurrentLine As Long, b As Long, c As Long, d As Long
Set MyCodePane = VBInstance.ActiveCodePane
Set MyCodeMod = MyCodePane.CodeModule
MyCodePane.GetSelection iCurrentLine, b, c, d
If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MsgBox "Jeah, cursor in procedure!"
MyCodeMod.InsertLines iCurrentLine, "'This is a comment"
End If
End Function
Any suggestion?
If you goal is to insert comments or even code, then you
could save a lot of time by download the free addin MZtools.
--
Jan Hyde
https://mvp.support.microsoft.com/profile/Jan.Hyde- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
Hi Jan,
I know the MZTools. My code-procedure was only a example. In the right
procedure i would like insert a variable.
If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MyCodeMod.InsertLines iCurrentLine, strText ' < Variable
End If
Well MZTools will do that too. I'm just wondering if it's
really necessary for you write your own addin.

You may just want to of course. In which case can you post
an example of the code before and after your addin runs so I
can be sure of what you want to achieve?





--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Alexander Mueller
2007-10-02 01:12:20 UTC
Permalink
Post by Oliver Scheiber
i have developed an AddIn for the VB-IDE using visual basic 6.0.
Now I would like to insert a comment, but only even if the cursor is
in a procedure.
I tried this code but it doesn't works fine, it always comes MsgBox
Public Function InsertComment()
Dim MyCodePane As CodePane
Dim MyCodeMod As CodeModule
Dim iCurrentLine As Long, b As Long, c As Long, d As Long
Set MyCodePane = VBInstance.ActiveCodePane
Set MyCodeMod = MyCodePane.CodeModule
MyCodePane.GetSelection iCurrentLine, b, c, d
If iCurrentLine <> b Or c <> d Then
MsgBox "Cursor not in procedure!"
Else
MsgBox "Jeah, cursor in procedure!"
MyCodeMod.InsertLines iCurrentLine, "'This is a comment"
End If
End Function
Any suggestion?
Afaics you misinterpret the returned line and column values
from GetSelection. According to the ObjectBrowser the signature
goes like:

Sub GetSelection
( _
StartLine As Long, StartColumn As Long, _
EndLine As Long, EndColumn As Long _
)

So the first arg is the first line of the whole selection and the third
is the last line of the selection. 2 and 4 equivalent for cols.
Startline and Endline and also StartCol and EndCol
will be the same if the selection is only a caret.
The values do not indicate directly whether the selection covers the
body of a procedure.
You need the CodeModule-objects ProcOfLine and ProcBodyLine
methods to check which procedure - if any - is at a certain line.

Also keep in mind that the selection may only partially cover a proc
and partially reside outside of it. It may also cover multiple procs.
Decide where to place the comment then.

The code below returns all procs selected.

MfG+HTH,
Alex



Public Function InsertComment()

Dim MyCodePane As CodePane
Dim MyCodeMod As CodeModule
Dim StartLine&, StartColumn&, EndLine&, EndColumn&

Dim strProcs() As String
Dim strProc As String
Dim flgProcKind As vbext_ProcKind
Dim i&, j&

Set MyCodePane = m_VBE.ActiveCodePane
Set MyCodeMod = MyCodePane.CodeModule

MyCodePane.GetSelection _
StartLine&, StartColumn&, EndLine&, EndColumn&

ReDim strProcs(0 To EndLine - StartLine)
j = -1

'Walk ALL lines selected to get ALL procs selected
For i = StartLine To EndLine
strProc = MyCodeMod.ProcOfLine(i, flgProcKind)
If Len(strProc) > 0 Then

'The Space between two procs also belongs to the proc below
'in the point of view of VBE, but not of the ordinary
'coder ;-), so test against the interspace

If i + 1 > MyCodeMod.ProcBodyLine(strProc, flgProcKind) Then
If j > -1 Then
'Only add a proc once
If strProc <> strProcs(j) Then
j = j + 1
strProcs(j) = strProc
End If
Else 'First match
j = j + 1
strProcs(0) = strProc
End If
End If
End If
Next
If j > -1 Then

ReDim Preserve strProcs(j)
MsgBox "Selection hits the following procedures:" _
& vbCrLf & Join(strProcs, vbCrLf)

'Code for commenting/inserting code goes here

Else

MsgBox "No procedures selected"
Erase strProcs

End If

End Function

Loading...