Run macro once when open document

J

Juergen N.

Hi!

I need a solution to execute a macro only once, when I open the document the
first time. When I open the same document again
(to make some changes) the macro should not be executed.
Maybe someone has a code-example?

thanks in advance,

Juergen N.
 
G

Graham Mayor

Something like

Sub AutoOpen()
Dim oVars As Variables
Dim vVar As Variant
Dim bExists As Boolean
Dim Opened As Integer
Set oVars = ActiveDocument.Variables
bExists = False
For Each vVar In oVars
If vVar.Name = "varOpened" Then
bExists = True
Exit For
End If
Next vVar
If bExists = False Then
oVars("varOpened").Value = 0
End If
Opened = oVars("varOpened").Value
If Opened = 0 Then
MsgBox "Document opened for the first time"
Else
MsgBox "Document previously opened"
End If
oVars("varOpened").Value = 1
ActiveDocument.Save
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins - Word MVP

If by open the document for the first time, you meant creating the document
by using File>New, then in the template from which the document is being
created, you should insert the macro with the name of Sub AutoNew()

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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