PDA

View Full Version : Solved: Auto run macro when document opens



JohnnyBravo
05-26-2006, 08:36 AM
I've recorded the following macro in Word 2003. As you can see, it's very simple, it takes you to the end of the document; inserts the date, inserts the time, and inserts two lines afterwards.

Now my only problem is that I want it to automatically run when the user opens the document. I thought the necessary function was called "autorun" but it's not working.

Sub Date_Time()
'
' Date_Time Macro
' Macro recorded 5/26/2006 by John
'
AutoRun
Selection.EndKey Unit:=wdStory
Selection.InsertDateTime DateTimeFormat:="dddd, MMMM dd, yyyy"
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="h:mm:ss am/pm"
Selection.TypeParagraph
Selection.TypeParagraph
End Sub

lucas
05-26-2006, 08:45 AM
Put this in the ThisDocument module:

Private Sub Document_Open()
Selection.EndKey Unit:=wdStory
Selection.InsertDateTime DateTimeFormat:="dddd, MMMM dd, yyyy"
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="h:mm:ss am/pm"
Selection.TypeParagraph
Selection.TypeParagraph
End Sub


Only problem is it will run everytime you open the document if you have saved it before...it will run again

JohnnyBravo
05-26-2006, 09:03 AM
Only problem is it will run everytime you open the document if you have saved it before...it will run again.

Thanks Lucas, I think this will work.

The user's request was for a document he could use where he didn't have to type in the date & time manually. In essense he wanted the document to run like a 'diary' where it keeps all his previous comments and then starts a new section by starting w/ the current date & time.

BTW, what does the Autorun do in VBA? I was obviously barking up the wrong tree there! :dunno

lucas
05-26-2006, 09:18 AM
Not sure Johnny but I think its just old school......I think you can still use it but it still has to go in the thisdocument module