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-18T11:02:59+00:00

    BTW, do you try to create something like this:

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-02-18T08:24:58+00:00

    Andreas.

    I worked out that the issue was that the subs were in the Form module. I didn't manage to find anywhere that fact is explained in the help and examples, but discovered it by trial and error.

    Thanks

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-02-18T00:09:53+00:00

    The Ribbon doesn't make sense in the context of a Form. I have a VBA form that is presented to the user when they open the template so they can enter a bunch of data that will then populate the document.

    Was this answer helpful?

    0 comments No comments
  4. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2016-02-17T11:27:24+00:00

    Public Sub FormCopy()

        Me.Copy

    End Sub

    And what should be Me in this context???

    Make a new file, add a regular module, paste the code into and execute sub Main.

    Then you can see that the "Add-Ins" tab appears in the ribbon and it shows the popup.

    But all that is really old style, why not use the ribbon?

    Andreas.

    Sub Main()

      Dim MainBar As CommandBar

      Dim ContextMenu As CommandBarControl, ContextItem As CommandBarButton

      Set MainBar = Application.CommandBars.Add("MyCommandBar", msoBarFloating, Temporary:=True)

      Set ContextMenu = MainBar.Controls.Add(msoControlPopup, Temporary:=True)

      MainBar.Visible = True

      With ContextMenu

        .Caption = "MyMenu"

        Set ContextItem = .Controls.Add(msoControlButton)

        With ContextItem

          .FaceId = 19

          .OnAction = "FormCopy"

          .Caption = "Copy"

        End With

      End With

    End Sub

    Public Sub FormCopy()

      MsgBox "Hello World"

    End Sub

    Was this answer helpful?

    0 comments No comments