I placed the createNewMenu code in the onload event of the report. The first time it runs then the menu is added and works fine. If I call the report again and try to
reload the createNewMenu, I get a Run-Time error '5' Invalid procedure call or argument. I added a MsgBox to the Report_Load (not shown) and if I choose not to run
the createNewMenu after the 1st time then it works fine. Where should I put the createNewMenu code and appears it should only be executed once.
Private Sub Report_Load()
Call createNewMenu
End Sub
Public Sub createNewMenu()
Dim cmbRightClick As Office.CommandBar
Dim cmbControl As Office.CommandBarControl
' Create the shortcut menu.
Set cmbRightClick = CommandBars.Add("cmdReportRightClick", msoBarPopup, False, True)
With cmbRightClick
' Add the Print command.
Set cmbControl = .Controls.Add(msoControlButton, 2521, , , True)
' Change the caption displayed for the control.
cmbControl.Caption = "Quick Print"
' Add the Print command.
Set cmbControl = .Controls.Add(msoControlButton, 15948, , , True)
' Change the caption displayed for the control
' Add the Page Setup... command.
Set cmbControl = .Controls.Add(msoControlButton, 247, , , True)
' Change the caption displayed for the control.
cmbControl.Caption = "Page Setup"
cmbControl.BeginGroup = True
' Add the PDF or XPS command.
Set cmbControl = .Controls.Add(msoControlButton, 12499, , , True)
' Change the caption displayed for the control.
cmbControl.Caption = "Save as PDF/XPS"
cmbControl.BeginGroup = True
Set cmbControl = .Controls.Add(msoControlButton, 106, , , True)
' Change the caption displayed for the control.
cmbControl.Caption = "Close Report"
End With
Set cmbControl = Nothing
Set cmbRightClick = Nothing
End Sub