VBA: How to make a word on close event macro run just once?

F

Frank

I have a on close event macro that adjust some tables in a word document when
it is closed for the first time.

How do I delete or disable the macro during the first document closing?

Is it possible to delete the macro itself as the last command in the macro?

Regards

Frank Krogh
 
G

Greg Maxey

Frank,

You could set a document variable when the document is first closed and
then use it to determine if it has been closed previously and bypass
the remaing code:

Sub Document_Close()
On Error GoTo Handler
If ActiveDocument.Variables("Flag").Value = "Closed" Then
Exit Sub
Else
ActiveDocument.Variables("Flag").Value = "Closed"
MsgBox "Do your deed"
End If
Exit Sub
Handler:
ActiveDocument.Variables("Flag").Value = " "
Resume
End Sub

AFAIK you can't delete the macro in the manner you suggest.
 
T

Tony Jollans

It _is_ possible (subject to the user allowing it in recent versions of
Word) to have self deleting code but it, in this case at least, it rather
begs the question as to how the code got into the document in the first
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