Hi All,
I'm new to the forum. My first thread concerns usage of custom controls added to VBE
Problem: to add functionality, which will allow vba programmer to insert tags (as commented statements) during design time via custom controls.
For example, I want to add the following tag:
' ================================================
' PURPOSE:
' INPUT:
' OUTPUT:
' ================================================
in the code module; tag should start at the current cursor position. This should be an analog to built-in functionality of commenting text via 'comment block' control
Solution to the problem
I'm adding control (CommandBarButton) to the CommandBar and "linking" it to the class, which is wrapping control click event
Code executed in "auto-open" formula to rebuild customized commandbar
Sub AddNewVBEControls()
Dim Ctrl As Office.CommandBarButton
Set Ctrl = Application.VBE.CommandBars.FindControl(Tag:=C_TAG)
Do Until Ctrl Is Nothing
Ctrl.Delete
Set Ctrl = Application.VBE.CommandBars.FindControl(Tag:=C_TAG)
Loop
Do Until EventHandlers.Count = 0
EventHandlers.Remove 1
Loop
Set MenuEvent = New CVBECommandHandler
With Application.VBE.CommandBars("Menu Bar")
Set CmdBarItem = .Controls.Add(msoControlButton)
End With
With CmdBarItem
.Caption = "insert method / property information tag"
.Tag = C_TAG
.Visible = True
.FaceId = 2950
.Style = msoButtonIconAndCaption
End With
Set MenuEvent.EvtHandler = Application.VBE.Events.CommandBarEvents(CmdBarItem)
EventHandlers.Add MenuEvent
End Sub
Class module code to wrap user click action
(Class module name = "CVBECommandHandler")
Public WithEvents EvtHandler As VBIDE.CommandBarEvents
Private Sub EvtHandler_Click(ByVal CommandBarControl As Object, _
handled As Boolean, CancelDefault As Boolean)
Dim lngStartLine As Long, lngStartColumn As Long
Dim lngEndLine As Long, lngEndColumn As Long
Const cstrLineString = "' ================================================" & _
vbCrLf & _
"' PURPOSE:" & _
vbCrLf & _
"' INPUT:" & _
vbCrLf & _
"' OUTPUT:" & _
vbCrLf & _
"' ================================================"
Application.VBE.ActiveCodePane.GetSelection lngStartLine, lngStartColumn, lngEndLine, lngEndColumn
Application.VBE.ActiveCodePane.CodeModule.InsertLines lngStartLine, cstrLineString
handled = True
CancelDefault = True
End Sub
Now, the problem is, that it is placing tag in the active code pane as it was expected to do, but only for the first time it is used. A have to run AddNewVBEControls method to have this working again, but still it will place the tag only at first click. After this first use there is no respond to this action, when I trying to place next tags.
Please kindly help me to resolve this problem. Thanks
Regards,
Kamil Z. (Poland)
---------- Post added at 04:01 PM ---------- Previous post was at 03:59 PM ----------
I've forgot about private properties in the codu module, which add custom commandbarbutton control:
Private MenuEvent As CVBECommandHandler
Private CmdBarItem As CommandBarButton
Public EventHandlers As New Collection
Private Const C_TAG = "MY_VBE_TAG"
Bookmarks