Limit a CommandBar Button Availability in Word

J

Jeffrey Grantz

I have added a commandbar button to my Word document and it works. However,
I want my button only visible in a subset of my open Word windows. How can
I turn the button visible for one group of Word windows and invisible for
others? I have tried using the WindowActivate and WindowDeactivate. The
deactivate does turn it off when I leave the window, but the Activate turns
it on whenever I go to a different Word window.



Thanks for any help.



Private Sub AppEventHandler_WindowActivate(ByVal Doc As Document, ByVal Wn
As Window)

Dim oMenuBar As CommandBar

Dim I As Integer

Set oMenuBar = CommandBars.Item("Standard")

For I = 1 To oMenuBar.Controls.Count

If I > 28 Then

I = I

End If

If oMenuBar.Controls(I).Caption = "&" + Hdr Then '"&MyGoTo" Then

'oMenuBar.Controls(I).Delete

oMenuBar.Controls(I).Visible = True

Exit For

End If

Next I

End Sub



Private Sub AppEventHandler_WindowDeactivate(ByVal Doc As Document, ByVal Wn
As Window)

Dim oMenuBar As CommandBar

Dim I As Integer

Set oMenuBar = CommandBars.Item("Standard")

For I = 1 To oMenuBar.Controls.Count

If I > 28 Then

I = I

End If

If oMenuBar.Controls(I).Caption = "&" + Hdr Then '"&MyGoTo" Then

'oMenuBar.Controls(I).Delete

oMenuBar.Controls(I).Visible = False

Exit For

End If

Next I

End Sub
 
J

Jonathan West

The simples approach to this sort ff thing is not to use code at all.
Instead, create a custom toolbar (commandbar) saved in your template, and
place buttons on that. The toolbar will only be available when a document
based on the template is the active document. If a document with a different
template is active, the toolbar disappears.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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