Custom Toolbars

N

Nigel

I am trying to ceate a custom toolbar for a word document. I had used some
code for excel which creates and deletes the toolbar as the form is opened
and closed,

Questions are as follows

1. How do I get the code to run when the document is opened or closed
2. How do I assign the macros to the buttons ( I can do it in excel but not
in word)

If I step thru the code it wll create the toolbar and hide the existing bars
BUt the caption doesn't change


'===========================================
Sub Auto_Close()
Call RemoveMenubar
End Sub

'===========================================
Sub RemoveMenubar()
On Error Resume Next
Application.CommandBars(ToolBarName).Delete
On Error GoTo 0
Application.CommandBars("Standard").Visible = True
Application.CommandBars("Formatting").Visible = True
Application.CommandBars("Control Toolbox").Visible = True
Application.CommandBars("Forms").Visible = True
End Sub

'===========================================
Sub CreateMenubar()

Dim iCtr As Long

Dim MacNames As Variant
Dim CapNamess As Variant
Dim TipText As Variant

Call RemoveMenubar

MacNames = Array("CommandButton21", "CommandButton11", "Showtoolbars",
"CommandButton3_Click")


CapNamess = Array("Reset Fields in Form", "Print Registration", "Show
Toolbars", "Save Changes")


With Application.CommandBars.Add
.Name = ToolBarName
.Left = 200
.Top = 200
.Protection = msoBarNoProtection
.Visible = True
.Position = msoBarTop

For iCtr = LBound(MacNames) To UBound(MacNames)
With .Controls.Add(Type:=msoControlButton)
.OnAction = MacNames(iCtr)
.Caption = CapNamess(iCtr)
.Style = msoButtonIconAndCaption
.FaceId = 71 + iCtr
End With
Next iCtr
End With
End Sub

Sub showtoolbars()
'
' Showtoolbar Macro
' Macro recorded 1/9/2007 by Nigel Bennett

If Application.CommandBars("Standard").Visible = True Then
GoTo group2
Else
Application.CommandBars("Standard").Visible = True
Application.CommandBars("Formatting").Visible = True
Application.CommandBars("Control Toolbox").Visible = True
Application.CommandBars("Focus").Visible = True


With Application.CommandBars.ActionControl
.Caption = "Hide Toolbars"
End With
Exit Sub
End If


'
group2:
'
Application.CommandBars("Standard").Visible = False
Application.CommandBars("Formatting").Visible = False
Application.CommandBars("Control Toolbox").Visible = False
Application.CommandBars("Focus").Visible = False


With Application.CommandBars.ActionControl
.Caption = "Hide Toolbars"
End With

End Sub
 
S

Shauna Kelly

Hi Nigel

Word and Excel work in mysteriously different ways when it comes to
toolbars.

In Word, you must explicitly identify where you want your toolbar saved. If
it's saved in a document, then you will only ever see the toolbar when you
view that document. If you save your toolbar in a template, then it will be
visible to all documents based on that template. And if you save it in a
global template (that is to say, an add-in) then it will be visible to all
documents. Unlike Excel, you don't have to handle the hiding and showing of
the toolbar: Word does it for you. In code, set the .CustomizationContext to
identify where to save the command bar.

If you want the toolbar's controls to run code, then you should obviously
store the code in the same place as the toolbar.

To add a button that invokes a macro to a toolbar button, do Tools >
Customize. Set the "Save in" box appropriately. On the Commands tab, in the
Categories list, find Macros. Now, in the Commands list you'll see all
macros you may put on a toolbar. You may now spend 5 minutes puzzling as to
why the box is not large enough to see the full names of your macros, but
there's nothing you can do about that, so move on<g>. Drag the macro name to
the toolbar. From here on, you can change the way the button looks in the
same way you can in Excel.

Unlike Excel, you can't re-assign a new macro to an existing button. You
must create a new button.

To get code to run when a document is opened or closed, do it the same way
you would in Excel: use the events in the ThisDocument class.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
N

Nigel

Thanks Shauna, thay explained a lot and I see now how word works, one further
question though, How do I get the toolbar to stay open, when I open the
document the toolbar pops up then dissapears and I have to re-open it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top