Share via

Command Bar Controls

Anonymous
2016-02-17T03:35:05+00:00

I'm building a custom commandbar in Word and can't seem to get it to work.

I've got the code building the bar and it is displaying it correctly, but when I click any of the CommandBarButtons, I get the following error.

Given that the file has VBA code and access to the VBA model is enabled, I'm at a loss as to what is causing the error. I've created my Procedures (Subs) in the same module as the code that set's up the commandbar and the code that calls it.

'Code to be called

Public Sub FormCopy()

    Me.Copy

End Sub

' code to create bar and first button

Set ContextMenu = Application.CommandBars.Add("FormContext", msoBarPopup, False, True)

With ContextMenu

    Set ContextItem = .Controls.Add(msoControlButton)

    With ContextItem

        .OnAction = "FormCopy"

        .Caption = "Copy"

    End With

Any suggestions please?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2016-02-18T08:15:01+00:00

I hate to play guessing games...

I guess Sub FormCopy is located in your form, that is a class module!

You can only execute public subs from within a regular module!

Make a new file, add a userform, add this sub:

Sub Hello_World()

  MsgBox "Hello World"

End Sub

add a regular module, add this code:

Sub MyHello_World()

  UserForm1.Hello_World

End Sub

Sub Fails()

  Application.Run "UserForm1.Hello_World"

  Application.Run "Hello_World"

End Sub

Sub Works()

  Application.Run "MyHello_World"

End Sub

The Commandbar Object calls Application.Run to execute the OnAction property.

Andreas.

Was this answer helpful?

0 comments No comments

10 additional answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2016-02-28T14:19:24+00:00

    Download this file and import it into your project:

    https://dl.dropboxusercontent.com/u/35239054/modFormContextmenu.bas

    The comments in the created module are in German, sorry, but you can use e.g. google translator.

    The use is very simple, see code below.

    Andreas.

    Private Sub ComboBox1_Enter()

      CcpAddUndo ComboBox1

    End Sub

    Private Sub ComboBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)

      If CcpMouseUp(ComboBox1, Button, Shift) Then Exit Sub

    End Sub

    Private Sub TextBox1_Enter()

      CcpAddUndo TextBox1

    End Sub

    Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)

      If CcpMouseUp(TextBox1, Button, Shift) Then Exit Sub

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-02-23T08:36:23+00:00

    Andreas, That is exactly what I'm trying to create!

    Any helpful links?

    Was this answer helpful?

    0 comments No comments