Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Add a Macro to the Toolbar Through VB

Posted on 2005-03-11
Medium Priority
Last Modified: 2010-08-05
I am trying to add a button that contains a macro to the toolbar in Word.  However, I keep getting an object required error when I run the following code:

 Toolbars.ToolbarButtons.Add ("Table of Contents")
 Toolbars("Table of Contents").ToolbarButtons.Add Button:=231, Before:=1
 Toolbars("Table of Contents").Visible = True
 Toolbars("Table of Contents").ToolbarButtons(1).OnAction = "TOC"

I'm not familiar with adding macros to the toolbar so any help is appreciated.
Question by:slattbr
LVL 22

Expert Comment

ID: 13522407

Author Comment

ID: 13524254
Pasted the code from the link and got the same object required error message.  Do I need to add a reference in order for this to work?
LVL 33

Expert Comment

by:Jeroen Rosink
ID: 13529431
you also might take a look at:

or here an example macro:

Sub AddButton()
      ' Dimension variables.
      Dim mytoolbar As String, mytooltip As String, mystatbar As String
      Dim used As Boolean, X As Integer

      ' Set variable used to value False.
      used = False

      ' Prompt for name of new toolbar to create.
      mytoolbar = InputBox("Enter name of new toolbar to add:")

      ' Check for duplicate toolbar name.
      For X = 1 To Application.Toolbars.Count
         If UCase(mytoolbar) = UCase(Application.Toolbars(X).name) Then
            ' If same name exists, set variable used to value True.
            used = True
         End If

      ' If toolbar name exists, prompt for name again.
      If used = True Then
         MsgBox "Sorry, this toolbar name is already being used." & _
            " Please enter name of toolbar that doesn't already exist."
         ' Display original dialog box asking for name.
         GoTo again
      End If

      ' Check for blank toolbar name entered.
      If mytoolbar = "" Then GoTo none

      ' Create a new toolbar with the name entered above.
      With Toolbars.Add(mytoolbar)

         ' Show the newly created toolbar.
         .Visible = True

         ' Add the "hand" button to the new toolbar
         ' and assign macro "myMacro" to the new button.
         ' Note that you can modify this line to add any button
         ' you want, and assign any existing macro.
         .ToolbarButtons.Add Button:=229, OnAction:="myMacro"

         ' Prompt for ToolTip to display for new button.
         mytooltip = _
            InputBox("Enter text of ToolTip to display for new button:")

         ' Add ToolTip to new button with name entered above.
         .ToolbarButtons(.ToolbarButtons.Count).name = mytooltip
      End With

      ' Prompt for text to display on the status bar when you
      ' hold the mouse over this button.
       mystatbar = InputBox("Enter text that you want to appear" & _
          " on the status bar for this button")

      ' Assign status bar text entered above to macro "myMacro"
      ' that is assigned to the new button.
      Application.MacroOptions Macro:="myMacro", StatusBar:=mystatbar


    End Sub

   ' Sample macro that is assigned to new button.
    Sub myMacro()
      MsgBox "This macro is assigned to your new toolbar button!"
    End Sub


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 13546384
There is no such thing as Application.Toolbars at least not in Word 97

Use CommandBars works similar way

Dim cb As CommandBarButton

CommandBars.Add ("Table of Contents")
Set cb = CommandBars("Table of Contents").Controls.Add(Type:=msoControlButton)
cb.FaceId = 17
cb.OnAction = "Your macro"

Author Comment

ID: 13555000

when I ran your code nothing happened.  however got the following code to create a button on the toolbar - problem is that I must manually click on text only (always) to see the name of the button.  Any ideas why this is happening?

Dim myBar As CommandBar

Set myBar = CommandBars.Add(Name:="Table of Contents", Position:=msoBarTop, Menubar:=True, Temporary:=True)

Set myControl = myBar.Controls.Add(Type:=msoControlButton)

With myControl
    .Caption = "Table of Contents"
    .OnAction = "TOC"
End With

myBar.Visible = True
LVL 12

Accepted Solution

gbzhhu earned 1200 total points
ID: 13555062
Try adding this

.Style = msoButtonCaption

within the with statement

Author Comment

ID: 13560910
Worked great, thanks.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question