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. Anonymous
    2016-02-17T06:56:30+00:00

    Thanks Jim but what is required is a right-click menu that works the same as it does in any normal dialog box.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-02-17T06:48:58+00:00

    Re:  call code from button

    I don't do Word, but I'll plunge ahead anyway...

    Add a button to the UserForm (from the toolbox) whose click event includes the FormCopy code.

    '---

    Jim Cone

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-02-17T06:07:56+00:00

    Thanks Jim.

    It's not actually a Ribbon that I'm trying to use, I'm building a contect menu to use on a VBA Form so the user can copy/cut/paste the text between controls.

    Not sure how else to provide "standard" functionality in a form.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-02-17T05:57:21+00:00

    Re:  commands bars in xl2013

    My opinion...

    MS doctored the 2013 release so that most command bar code no longer works.

    You have to use XML code with Office 2013+ to alter the Ribbon,

    or you can go back to Office 2010 ( I never left).

    '---

    Jim Cone

    Portland, Oregon USA

    free & commercial excel add-in programs  (xl97 to xl2010 only)

    http://jmp.sh/K95N3ee

    Was this answer helpful?

    0 comments No comments