Closing custom toolbar when document closes



I have a custom toolbar which appears when a document is
created using a template. How do I close this custom
toolbar when the document closes as this toolbar is only
associated with that template/document.

Code for creating/showing toolbar:

Sub CreateToolbar()

Dim cbar As CommandBar, cbct1 As CommandBarControl

'Create a floating toolbar
Set cbar = CommandBars.Add(Name:="Agenda Hyperlink",
cbar.Visible = True

'Add a custom button control to execute a macro
Set cbct1 = cbar.Controls.Add(Type:=msoControlButton)
cbct1.Visible = True
cbct1.Style = msoButtonCaption
cbct1.Caption = "Add Hyperlink"
'Run the following macro
cbct1.OnAction = "Hyperlink"

End Sub

Sub Hyperlink()

Dim fd As FileDialog, displaytext As String
'Create a FileDialog object as a File Picker dialog
Set fd = Application.FileDialog
'Use a With...End With block to reference the
FileDialog object.
With fd
'Set the initial path to the Agenda Attachments
= "\\alchemy\data\processes\documents\agenda attachments\"
.Title = "Select the File to which you want to
create the link"
'Use the Show method to display the File Picker
dialog box and return the user's action.
'If the user presses the action button...
If .Show = -1 Then
displaytext = .SelectedItems(1)
While InStr(displaytext, "\") > 0
displaytext = Mid(displaytext, InStr
(displaytext, "\") + 1)
displaytext = Left(displaytext, Len
(displaytext) - 4)
Anchor:=Selection.Range, Address:=.SelectedItems(1),
' 'If the user presses Cancel...
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing

End Sub

I tried to create a custom toolbar by right-clicking on a
toolbar in Word (2002), Customize, Commands, Macros and
there should of been an option to create a custom toolbar
but there isn't!!!

Help anyone...





Hi Lee
The closing of the toolbar is simply the reverse of what
you're already doing.
cbar.Visible = True (Set to False in order to close it)

You've obviously figured out how to use the Document_Open
() event, so just use the close event to execute the

As to your question regarding a custom toolbar, you'll
find the ability to add a toolbar, not under
the "Commands" tab of the customize menu, but
the "Toolbars" tab - Click "New..".


Jean-Guy Marcil


Dans son message, < Lee > écrivait :
In this message, < Lee > wrote:

|| I have a custom toolbar which appears when a document is
|| created using a template. How do I close this custom
|| toolbar when the document closes as this toolbar is only
|| associated with that template/document.

If a toolbar is linked to a template, then creating a document from that
template will automatically display that toolbar. While working on that
document, the toolbar will be present, unless the user closes it,of course.
If a new document is created from another template, then the toolbar will
only be displayed when the document based on the template containing the
toolbar is activated, so when switching between documents, the toolbar will
be shown or hidden based on the active document template. So, if you close
the document based on that template, and no other documents from that
template are active, the toolbar should disappear. If it does not, it means
the toolbar is not contained in the template used to create the said
document, most likely it is residing in

When programmatically creating menus and toolbars, make sure to set the
CustomizationContext to the attached template, otherwise modifications will
be saved in From the VBA editor, type CustomizationContext,
select it and hit F1 for info on this property.

Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:

Charles Kenyon

Unless the toolbar is different each time you use it, why not simply create
it once in your template and then it will be available to all documents
using the template.

Also, I did not see anything in your code setting the customization context.
In that case, you are probably making changes to, which is _not_


It is the same each time but I am unsure how to create a
custom toolbar in my template as I cannot create a custom
button within Word (e.g. View - Toolbars - Customize -
Commands - Macros - Custom Button).

I found the "Create Toolbar" code in a VBA book and
customised it...

Have add customization context setting - didn't know
anything about this... ohhhhh I have a lot to learn!!




Charles Kenyon

Create a custom toolbar (in your template).

Under Macros, pick yours and slide it to your toolbar. Change the button to

See <URL:> for step-by-step
instructions on moving / sharing / copying customizations including
AutoText, AutoCorrect, keyboard assignments, macros, etc.
Charles Kenyon

Word New User FAQ & Web Directory:

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)

Word Resources Page

See also the MVP FAQ: which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.


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