J
JH
Folks,
I am working in Access 2003 and trying to add some new functions to the VBA
editor. I followed the instructions given at
www.cpearson.com/excel/vbemenus.htm and the examples worked well.
Then I started to modify them to insert lines of code into modules. Whilst
the insertion works, it only works once, and then the new menu item just
does nothing and I have to recreate it, calling 'AddNewMenuItems' directly,
I even tried calling this sub from the module that inserts lines, but that
didn't seem to work either . Any clues what is happening and how I get
around the problem?
Cheers,
John
The code is:
One module:
Public ID As Integer
Public MnuEvt As VBECmdHandler
Public CmdItem As CommandBarControl
Public EvtHandlers As New Collection
Sub AddNewMenuItems()
While EvtHandlers.Count > 0
EvtHandlers.Remove 1
Wend
With Application.VBE.CommandBars("MEnu Bar")
.Reset
Set CmdItem = .Controls.Add
ID = CmdItem.ID
CmdItem.Caption = "Tra&p Errors"
CmdItem.BeginGroup = True
CmdItem.OnAction = "InsCodeLines"
CmdItem.Style = msoButtonCaption
Set MnuEvt = New VBECmdHandler
Set MnuEvt.EvtHandler = Application.VBE.Events.CommandBarEvents(CmdItem)
EvtHandlers.Add MnuEvt
End With
End Sub
Sub InsCodeLines()
Dim lngLine As Long
Dim strProcName As String
Dim lngBody As Long
VBE.ActiveCodePane.GetSelection lngLine, 1, lngLine, 1000
strProcName = VBE.ActiveCodePane.CodeModule.ProcOfLine(lngLine,
vbext_pk_Proc)
lngBody = VBE.ActiveCodePane.CodeModule.ProcBodyLine(strProcName,
vbext_pk_Proc)
VBE.ActiveCodePane.CodeModule.InsertLines lngLine + 1, "'Insert this text"
AddNewMenuItems
And one Class Module called VBECmdHandler
Public WithEvents EvtHandler As VBIDE.CommandBarEvents
Private Sub EvtHandler_Click(ByVal CommandBarControl As Object, _
Handled As Boolean, CancelDefault As Boolean)
On Error Resume Next
'
' Run the code specified in the object's OnAction property.
'
Application.Run CommandBarControl.OnAction
' Indicate to the Events object that we've successfully handled the event.
'
Handled = True
CancelDefault = True
End Sub
I am working in Access 2003 and trying to add some new functions to the VBA
editor. I followed the instructions given at
www.cpearson.com/excel/vbemenus.htm and the examples worked well.
Then I started to modify them to insert lines of code into modules. Whilst
the insertion works, it only works once, and then the new menu item just
does nothing and I have to recreate it, calling 'AddNewMenuItems' directly,
I even tried calling this sub from the module that inserts lines, but that
didn't seem to work either . Any clues what is happening and how I get
around the problem?
Cheers,
John
The code is:
One module:
Public ID As Integer
Public MnuEvt As VBECmdHandler
Public CmdItem As CommandBarControl
Public EvtHandlers As New Collection
Sub AddNewMenuItems()
While EvtHandlers.Count > 0
EvtHandlers.Remove 1
Wend
With Application.VBE.CommandBars("MEnu Bar")
.Reset
Set CmdItem = .Controls.Add
ID = CmdItem.ID
CmdItem.Caption = "Tra&p Errors"
CmdItem.BeginGroup = True
CmdItem.OnAction = "InsCodeLines"
CmdItem.Style = msoButtonCaption
Set MnuEvt = New VBECmdHandler
Set MnuEvt.EvtHandler = Application.VBE.Events.CommandBarEvents(CmdItem)
EvtHandlers.Add MnuEvt
End With
End Sub
Sub InsCodeLines()
Dim lngLine As Long
Dim strProcName As String
Dim lngBody As Long
VBE.ActiveCodePane.GetSelection lngLine, 1, lngLine, 1000
strProcName = VBE.ActiveCodePane.CodeModule.ProcOfLine(lngLine,
vbext_pk_Proc)
lngBody = VBE.ActiveCodePane.CodeModule.ProcBodyLine(strProcName,
vbext_pk_Proc)
VBE.ActiveCodePane.CodeModule.InsertLines lngLine + 1, "'Insert this text"
AddNewMenuItems
And one Class Module called VBECmdHandler
Public WithEvents EvtHandler As VBIDE.CommandBarEvents
Private Sub EvtHandler_Click(ByVal CommandBarControl As Object, _
Handled As Boolean, CancelDefault As Boolean)
On Error Resume Next
'
' Run the code specified in the object's OnAction property.
'
Application.Run CommandBarControl.OnAction
' Indicate to the Events object that we've successfully handled the event.
'
Handled = True
CancelDefault = True
End Sub