Macro that runs when document is first saved

G

Guest

Is it possible to set up a macro to run when a document is first saved (and
not everytime it is subsequently saved)? Thanks.
 
J

Jay Freedman

Is it possible to set up a macro to run when a document is first saved (and
not everytime it is subsequently saved)? Thanks.

Not exactly, but you can achieve the same effect. Write the macro to
do whatever you want on the first save, and include this as the first
line of the macro:

If Len(ActiveDocument.Path) > 0 Then Exit Sub

When a document is new and never saved before, its .Path parameter is
an empty string with a length of zero. Once it has been saved, the
..Path is the complete path from the disk letter to the folder where it
was saved. So with this line in place before any other actions, if the
document has been saved the macro will exit immediately and not do
anything else.
 
G

Guest

Thanks Jay. I tried inserting the line you suggested. However, on the first
save, the macro doesn't appear to have run. The macro basically finds the
phrase 'taxtotal' and replaces it with a formula. Its the replacement with
the formula that I want to happen automatically on the first save. Any
suggestions would be greatly appreciated.

This is the macro:
If Len(ActiveDocument.Path) > 0 Then Exit Sub
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "taxtotal"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"= sum(a2:a4)*.10\#""$,0.00;;""", PreserveFormatting:=False
 
S

Suzanne S. Barnhill

I guess I'm just stupid, but wouldn't a FileSaveAs macro do it (since
subsequent saves would be FileSave)?

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Jay Freedman said:
Not exactly, but you can achieve the same effect. Write the macro to
do whatever you want on the first save, and include this as the first
line of the macro:

If Len(ActiveDocument.Path) > 0 Then Exit Sub

When a document is new and never saved before, its .Path parameter is
an empty string with a length of zero. Once it has been saved, the
.Path is the complete path from the disk letter to the folder where it
was saved. So with this line in place before any other actions, if the
document has been saved the macro will exit immediately and not do
anything else.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
 
G

Graham Mayor

Would this have anything to do with the form referred to in your later post?
In which case tell us more about what you are doing.

1. Why don't you just put the field in the table to begin with?
2. The following macro will do what you want

Sub FileSave()
If Len(ActiveDocument.Path) > 0 Then GoTo AllDone:

With Selection
With .Find
.ClearFormatting
.Text = "taxtotal"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
With .Fields
.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"= sum(a2:a4)*.10 \#""$,0.00;;""", PreserveFormatting:=False
.Update
End With
End With
ActiveWindow.View.ShowFieldCodes = False

AllDone:
ActiveDocument.Save
End Sub


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jay Freedman

It's counterintuitive, but the answer is no. Although the first save
behaves like FileSaveAs (opening the Save As dialog), it doesn't
actually execute a FileSaveAs command, so that macro wouldn't run.
 
S

Suzanne S. Barnhill

Interesting.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Jay Freedman said:
It's counterintuitive, but the answer is no. Although the first save
behaves like FileSaveAs (opening the Save As dialog), it doesn't
actually execute a FileSaveAs command, so that macro wouldn't run.



--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
 
G

Guest

Thanks Graham for your suggestions. The reason I can't put the field in the
table to begin with is that the table is in a template that is merged with
our document automation software, which seems to strip all fields out of a
document upon merging? Hence, my idea to write a macro is run straight after
merging (first save?) that searches for the word 'taxtotal' and replaces it
with a formula field. At the moment, I've created a toolbar button for a
user to click that runs the macro, but ideally I'd like it to run
automatically. Any suggestions about how to achieve this would be greatly
appreciated. Maybe another event besides first save?
 
G

Graham Mayor

The macro in my previous post, if saved in the document template, runs on
pressing the save button or on choosing save from the file menu.
If your existing macro saves the document then it is never going to work as
the document is always saved before it runs. In that case you need to add
the search routine to your existing macro - before it saves the document.


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

My web site www.gmayor.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