How to make a word macro to execute only once?

L

Luca.albanesi

Hi,

I've got this unusual problem to solve.

I have a Word Document Template (.dot) that is used by my co-workers
to prepare offers for customers. This adds on top and bottom of the
document our company logo and a few contact infos.

My co-workers asked me if I can show an alert when they close the
document, asking "Do you want to save this document in the 'offers'
directory? (Y/N)" and I made this through a macro, using the "close"
event of the document. There are a few problems, though:

- This prompt happens everytime the user closes the file
- This must not be displayed if the customer opens and closes it

My idea was to make it execute only once, so that the second time it
won't be displayed... but how do I do it? How can I set a flag or
something so that a macro is executed only once, or how can I delete a
macro from scripts in VBA?

Thanks in advance!
 
G

Gordon Bentley-Mix

Luca,

Perhaps something like this would work:

Option Explicit

Sub AutoClose()
If fcnFindVar("SavedToOffers") = False Then
ActiveDocument.Variables.Add "SavedToOffers", "no"
End If
If LCase(ActiveDocument.Variables("SavedToOffers").Value) = "no" Then
Dim myResult As Integer
myResult = MsgBox("Do you want to save to the 'offers' folder?",
vbYesNo, "Save Document")
If myResult = 6 Then
Dim myFilePath As String
Dim myFileName As String
myFilePath = "X:\Offers\"
myFileName = ActiveDocument.BuiltInDocumentProperties("Title")
ActiveDocument.Variables("SavedToOffers").Value = "yes"
ActiveDocument.SaveAs myFilePath & myFileName
End If
End If
End Sub

Private Function fcnFindVar(VarName As String) As Boolean
Dim myVar As Variable
For Each myVar In ActiveDocument.Variables
If myVar.Name = VarName Then
fcnFindVar = True
Exit For
End If
Next myVar
End Function

There might be a bit more to do with how you actually execute the save to
ensure that you don't overwrite an existing document, etc. And of course you
would need to replace myFilePath with the path to your "offers" folder and
myFileName with something a bit more meaningful. However, this should give
you an idea of where to begin.
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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