PDA

View Full Version : ContextMenu example errors



SimonHA
03-05-2013, 03:23 PM
Hello everyone,
I'm completely new to VBA and I'm trying to add a contextmenu with simple code from MSDN.
But I'm getting error BC30469: Reference to a non-shared member requires an object reference.
This appears at MyContext = Application.CommandBars("Cell")

What does that error mean?

Sub AddCellContext()

Dim MyContext As CommandBars
Dim MySubMenu As CommandBarControl

' Set MyConext to the Cell context menu.
MyContext = Application.CommandBars("Cell")

' Add one built-in button(Save = 3) to the Cell context menu.
MyContext.Controls.Add(Office.MsoControlType.msoControlButton, Id:=3, Before:=1)

' Add a custom submenu with three buttons.
MySubMenu = MyContext.Controls.Add(Office.MsoControlType.msoControlButtonPopup, Before:=3)

With MySubMenu
.Caption = "Test"
.Tag = "Cell_Test"

With .Controls.Add(Office.MsoControlType.msoControlButton)
.FaceId = 100
.Caption = "Test 1"
End With
With .Controls.Add(Office.MsoControlType.msoControlButton)
.FaceId = 91
.Caption = "Test 2"
End With
With .Controls.Add(Office.MsoControlType.msoControlButton)
.FaceId = 95
.Caption = "Test 3"
End With
End With

' Add a separator to the Cell context menu.
MyContext.Controls(4).BeginGroup = True

End Sub

Regards,

Simon H.A.

Paul_Hossler
03-05-2013, 05:37 PM
I think that since your MyContext is an object, you'll need to use Set to set (pun intended) it


Set MyContext = Application.CommandBars("Cell")


Paul

SimonHA
03-06-2013, 03:25 AM
My compiler says that Set and Let are no longer supported. I'm using Visual Studio 2010.

Regards,

Simon H.A.

Aflatoon
03-06-2013, 05:48 AM
Visual Studio is not VBA. ;)

Are you writing an add-in or a separate program? You need to get a reference to the Excel.Application object rather than just referring to Application as you can in VBA running inside Excel.