?
Solved

Add a Macro to the Toolbar Through VB

Posted on 2005-03-11
7
Medium Priority
?
317 Views
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.
0
Comment
Question by:slattbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Expert Comment

by:Dreamboat
ID: 13522407
0
 

Author Comment

by:slattbr
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?
0
 
LVL 33

Expert Comment

by:Jeroen Rosink
ID: 13529431
you also might take a look at:
http://www.exceltip.com/excel_tips/Menus,_Toolbars,_Status_bar_in_VBA/205.html
http://support.microsoft.com/default.aspx?scid=kb;en-us;146863
http://support.microsoft.com/default.aspx?scid=kb;en-us;159619

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

again:
      ' 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
      Next

      ' 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

none:

    End Sub

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

Cheers

Jeroen
0
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

by:gbzhhu
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"
0
 

Author Comment

by:slattbr
ID: 13555000
gbzhhu-

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
0
 
LVL 12

Accepted Solution

by:
gbzhhu earned 1200 total points
ID: 13555062
Try adding this

.Style = msoButtonCaption

within the with statement
0
 

Author Comment

by:slattbr
ID: 13560910
Worked great, thanks.
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
New style of hardware planning for Microsoft Exchange server.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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…

764 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