Add-in prompts to save changes

M

mmalinsky

I have the following code that assigns the numpad decimal to a comma.
Most of the code came from another posting in this NG and I added some
code to turn this on/off using a toolbar button using the msoButtonUp
and msoButtonDown properties to indicate if this feature is turned off
or on. The problem I'm running into is that if I have the feature
turned on (msoButtonDown) and close a document, a get a prompt asking
is I want to save the changes to the template. I'm assuming this is
because the original state of the button is up and when I close the
docment the button is down. I thought I could use a BeforeClose event
to eliminate this, but it doesn't seem to work. Any help is
appreciated. The code is below.

Thanks,
Mike.

Private Sub AssignNumpadPeriodToComma()

'Assigns/unassigns numpad decimal to comma
'using a toolbar button. Thanks to a posting
'by Mark Tangard in the Word VBA users group
'http://groups.google.com/group/microsoft.public.word.vba.general/
browse_thread/thread/17a3ede7fe2866b1/1e742ff147e0abf2?
hl=en&lnk=st&q=wdKeyNumericDecimal&rnum=1#1e742ff147e0abf2

If FindKey(BuildKeyCode(wdKeyNumericDecimal)).KeyCategory = -1
Then
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
KeyCode:=BuildKeyCode(wdKeyNumericDecimal), _
Command:="TypeAComma"
CommandBars("Helpers").Controls(1).State = msoButtonDown
Else
If FindKey(BuildKeyCode(wdKeyNumericDecimal)).KeyCategory = 2
Then
FindKey(BuildKeyCode(wdKeyNumericDecimal)).Clear
CommandBars("Helpers").Controls(1).State = msoButtonUp
End If
End If


End Sub

Sub TypeAComma()

Selection.TypeText ","

End Sub

Private Sub DocumentBeforeClose()

ThisDocument.Saved = False

End Sub
 
T

Tony Jollans

I guess it depends a little on whether you want to save the key binding
(along with the button state) but, assuming that you don't, you can just set
the template as Saved immediately after making the change. Better, however,
to restore the saved state to what it was before you made your change -
otherwise other customizations that the user made might get lost.

SavedState = ThisDocument.Saved
' Do your customizations
ThisDocument.Saved = SavedState

(incidentally, your posted code always sets it to false even if there is no
change)
 
M

mmalinsky

This doesn't seem to completely work...the button state is always up
when I open Word, but the key binding value saves. My current
workaround is a autoexec routine to clear the binding, but I was
hoping to do it some other way, if possible. Any suggestions?

Thanks.
 
T

Tony Jollans

Your "Helpers" toolbar is in your template.
Your keyBinding is explicitly in the Normal Template.

There isn't really any way you can guarantee the two remain in sync, unless
you keep them in the same place.
 

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