M
meh2030
I'm working with Excel 2003, and I'm trying to determine if there is a
way to get the text in a VBE toolbar control. Specifically, I'm
referencing the Standard toolbar in VBE, control ID 3201 (or at least
I believe this is the right control). This control lists the line and
column for which the cursor is currently located within the code
window, e.g. "Ln 7, Col 30". I used the FindCtrlIdViaToolbarName
procedure below to determine this control ID. If you run the
procedure you'll notice that the control is added to the newly created
toolbar as a button with the caption of "Gauge" and does not look like
the control on the native VBE Standard toolbar. I set up the
GetCtrlText procedure to try and determine if I can use a property to
return the text within this control.
I'm led to believe that it may not be possible to get the text in this
control. As a result, I'm looking for some way to get the code line
for which the cursor, or some other text marker (e.g. "ThisCodeLine"),
is in.
What I mean by text marker is this:
1 | Sub TestCodeLine()
2 | Dim myText As String
3 | myText = "ThisCodeLine"
4 | End Sub
The code line would be 3 for the text marker "ThisCodeLine."
I don't have any experience with coding VBE, but given that there is a
".CodeModule.Lines(StartLine, Count)" construct, I'm led to believe
that there may be a property somewhere to return what I'm looking
for. I'm still digging around.
Thanks,
Matthew Herbert
Sub FindCtrlIdViaToolbarName()
Dim myBarName As String
Dim nativeTBarName As String
Dim nativeCBar As CommandBar
Dim nativeCtrl As CommandBarControl
Dim chgTooltip As Boolean
Dim i As Long
Dim cBar As CommandBar
Dim ctrl As CommandBarControl
nativeTBarName = "Standard"
myBarName = "ID via Toolbar Name"
For Each cBar In Application.VBE.CommandBars
If cBar.Name = myBarName Then
cBar.Delete
End If
Next
Set cBar = Application.VBE.CommandBars.Add(myBarName, msoBarFloating,
False, True)
cBar.Visible = True
chgTooltip = True
i = 0
For Each nativeCBar In Application.VBE.CommandBars
If nativeCBar.Name = nativeTBarName Then
For Each nativeCtrl In nativeCBar.Controls
i = i + 1
Debug.Print i; " | Native Name:"; nativeCtrl.TooltipText;
" | ID:"; nativeCtrl.ID
'some controls won't add and the TooltipText won't change
either
''so I added the On Error statement
On Error Resume Next
cBar.Controls.Add ID:=nativeCtrl.ID
If chgTooltip Then
cBar.Controls(i).TooltipText = nativeCtrl.ID
End If
Next
End If
Next
End Sub
Sub GetCtrlText()
Dim myID As Long
Dim myCtrl
myID = 3201
Set myCtrl = Application.VBE.CommandBars.FindControl(ID:=myID)
'Can't seem to find a property that will return the text in the
control
Debug.Print myCtrl.Caption
End Sub
way to get the text in a VBE toolbar control. Specifically, I'm
referencing the Standard toolbar in VBE, control ID 3201 (or at least
I believe this is the right control). This control lists the line and
column for which the cursor is currently located within the code
window, e.g. "Ln 7, Col 30". I used the FindCtrlIdViaToolbarName
procedure below to determine this control ID. If you run the
procedure you'll notice that the control is added to the newly created
toolbar as a button with the caption of "Gauge" and does not look like
the control on the native VBE Standard toolbar. I set up the
GetCtrlText procedure to try and determine if I can use a property to
return the text within this control.
I'm led to believe that it may not be possible to get the text in this
control. As a result, I'm looking for some way to get the code line
for which the cursor, or some other text marker (e.g. "ThisCodeLine"),
is in.
What I mean by text marker is this:
1 | Sub TestCodeLine()
2 | Dim myText As String
3 | myText = "ThisCodeLine"
4 | End Sub
The code line would be 3 for the text marker "ThisCodeLine."
I don't have any experience with coding VBE, but given that there is a
".CodeModule.Lines(StartLine, Count)" construct, I'm led to believe
that there may be a property somewhere to return what I'm looking
for. I'm still digging around.
Thanks,
Matthew Herbert
Sub FindCtrlIdViaToolbarName()
Dim myBarName As String
Dim nativeTBarName As String
Dim nativeCBar As CommandBar
Dim nativeCtrl As CommandBarControl
Dim chgTooltip As Boolean
Dim i As Long
Dim cBar As CommandBar
Dim ctrl As CommandBarControl
nativeTBarName = "Standard"
myBarName = "ID via Toolbar Name"
For Each cBar In Application.VBE.CommandBars
If cBar.Name = myBarName Then
cBar.Delete
End If
Next
Set cBar = Application.VBE.CommandBars.Add(myBarName, msoBarFloating,
False, True)
cBar.Visible = True
chgTooltip = True
i = 0
For Each nativeCBar In Application.VBE.CommandBars
If nativeCBar.Name = nativeTBarName Then
For Each nativeCtrl In nativeCBar.Controls
i = i + 1
Debug.Print i; " | Native Name:"; nativeCtrl.TooltipText;
" | ID:"; nativeCtrl.ID
'some controls won't add and the TooltipText won't change
either
''so I added the On Error statement
On Error Resume Next
cBar.Controls.Add ID:=nativeCtrl.ID
If chgTooltip Then
cBar.Controls(i).TooltipText = nativeCtrl.ID
End If
Next
End If
Next
End Sub
Sub GetCtrlText()
Dim myID As Long
Dim myCtrl
myID = 3201
Set myCtrl = Application.VBE.CommandBars.FindControl(ID:=myID)
'Can't seem to find a property that will return the text in the
control
Debug.Print myCtrl.Caption
End Sub