Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Run macro once when open document

15 views
Skip to first unread message

Juergen N.

unread,
Jul 16, 2009, 8:09:42 AM7/16/09
to
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.


Graham Mayor

unread,
Jul 16, 2009, 9:26:20 AM7/16/09
to
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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org

Juergen N.

unread,
Jul 16, 2009, 10:11:41 AM7/16/09
to
Hello Graham,

thanks for the example. I'll have a try.

best regards,

J�rgen N.

"Graham Mayor" <gma...@REMOVETHISmvps.org> schrieb im Newsbeitrag
news:ucLZBkhB...@TK2MSFTNGP04.phx.gbl...

Doug Robbins - Word MVP

unread,
Jul 16, 2009, 5:49:20 PM7/16/09
to
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
"Juergen N." <j...@bauexpert.it> wrote in message
news:OWkqY9hB...@TK2MSFTNGP02.phx.gbl...

0 new messages