Hi there
[...]
I have tried VBA help, read the articles by Bill Coan, searched the msdn
site, and searched using Google, but have not found anything that talks
about how to do this. Seems like a closely guarded secret : )
[...] --> full quote below
Well, the thing with attaching to the Word-event WindowSelectionChange
is not such a hard thing to do. Here's one way to do achieve what you want:
1. Create a new template and save it in Word's startup folder (such
a template is called an "AddIn")
2. Create a class module (you MUST use a class module!!!) which will
be attached to word events and handle the change of the button state:
' class module "clsSelectionObserver"
Option Explicit
' using "WithEvents" is crucial here!
Dim WithEvents m_wd As Word.Application
' [Note that you can't use WithEvents outside a class module,
' so we are forced to use one]
' get a reference to the running copy of the application
Private Sub Class_Initialize()
Set m_wd = Application
End Sub
' release the reference
Private Sub Class_Terminate()
Set m_wd = Nothing
End Sub
' handle the selection change event
Private Sub m_wd_WindowSelectionChange(ByVal Sel As Selection)
Dim cmd As CommandBarButton
' adjust the button on the command bar
' NOTE: update the following line to access the correct button
Set cmd = CommandBars("SelectionObserver").Controls(1)
' adjust the button state to the selection
cmd.State = IIf(Sel.Font.Shadow, msoButtonDown, msoButtonUp)
End Sub
3. create a code module to instantiate and release the observer class
Option Explicit
Dim MyObserver As clsSelectionObserver
Public Sub StartObserver()
Set MyObserver = New clsSelectionObserver
End Sub
Public Sub EndObserver()
Set MyObserver = Nothing
End Sub
4. update the ThisDocument-class module with the following code
Option Explicit
Private Sub Document_Open()
Call StartObserver
End Sub
Private Sub Document_Close()
Call EndObserver
End Sub
5. write/adjust the macro to switch the shadow-state of the selection,
such that it fires the selection change event (needed in order to
immediately update the button's state)
Public Sub applyShadow()
Selection.Font.Shadow = Not Selection.Font.Shadow
' force event to be fired in order to update the button
Selection.Range.Select
End Sub
6. copy/create the commandbar to/in the newly created template
and place a button for the macro applyShadow on it
7. Save the template, restart Word and enjoy the show
Cheers,
Martin
Art® said:
I have tried VBA help, read the articles by Bill Coan, searched the msdn
site, and searched using Google, but have not found anything that talks
about how to do this. Seems like a closely guarded secret : )
Art