Hi Gordon
The WindowSelectionChange event won't fire when the user negotiates with the
cursor instead of the mouse, and it doesn't fire if the user clicks the
Document Map to move around. To get around that, you can use the built-in
functionality that Word has in its various buttons.
Find a built-in button that has the functionality you want. That is, find a
built-in button that turns on and off when you want yours to turn on and
off. (For example, if you want a button that is only enabled when the
insertion point is in a table, then choose one of the buttons on the Table
menu that operates as you want.)
Then *copy* the button you found to your own toolbar. You can then change
the icon and the displayed text to suit yourself. If you need more than one
button to be enabled and unenabled at the same time, but do different
things, then copy the *same* button (and change the icon and displayed text
to suit yourself).
Use the Immediate Pane to determine the ID of the button (let's say it's
296). Using the Immediate Pane, give your button(s) a .Tag (something
meaningful to you that identifies it as belonging to your project, let's say
"Gordon"). You may have several buttons with the same .ID and .Tag
combination. Give each button a separate .Parameter, which identifies each
one uniquely (let's say "Gordon_RestartNumbering" and
"Gordon_ContinueNumbering"). In my example here I used the useless names of
"Param1" and "Param 2".
So, in my example document I now have a custom toolbar named "Custom 1" with
two buttons. They both have the same .ID of 296. They both have a .Tag of
"Gordon". One has .Parameter "Param1" and the other is "Param2". And if I
play with Word, they turn on and off as I need them to.
Now use the following code:
1. In an ordinary Module:
Option Explicit
Public gMyButton As CMyButtons
2. In the ThisDocument class:
Option Explicit
Private Sub Document_New()
Set gMyButton = New CMyButtons
End Sub
Private Sub Document_Open()
Set gMyButton = New CMyButtons
End Sub
3. In a Class module named CMyButtons
Option Explicit
'A variable to hold a reference to any button with your combination
'of ID and Tag
Private WithEvents mMyButton As Office.CommandBarButton
Private Sub Class_Initialize()
'Set mMyButton to refer to a button with your combination of ID
'and Tag. Even though you may have many buttons with the same ID
'and Tag combination, you only need to 'hook' one of them here.
'This way is fast
Set mMyButton = CommandBars("Custom 1").Controls(1)
'OR....
'This way is safe
Set mMyButton = CommandBars.FindControl(Type:=msoControlButton, ID:=296,
Tag:="Gordon")
End Sub
Private Sub mMyButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
'This fires when the user clicks one of your buttons with your combination
'of ID and Tag
If Ctrl.Tag = "Gordon" And Ctrl.ID = 296 Then
'Strictly speaking you don't need this whole If ... .Tag *and* .ID
thing here
'but I find it enormously helpful documentation!
Select Case Ctrl.Parameter
Case "Param1"
MsgBox "You clicked my Param1 button"
'Run the appropriate code here
Case "Param2"
MsgBox "You clicked my Param2 button"
'Run the appropriate code here
End Select
'prevent Word from doing the default action
'for the button
CancelDefault = True
End If
End Sub
Create a new document from your template and watch the magic happen!
For the record, any .OnAction value of the button(s) is ignored when you
hook buttons like this.
Now, go buy a copy of Professional Excel Development by Bullen, Bovey and
Green where you'll read a much better description of this than I have given.
Ignore the early chapters about Excel, if you want to. The book is worth it
for the latter chapters and it applies equally well to Word as to Excel.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
(England v the Springboks. Who would have thought?)