"on save" macro launching

D

Dariusz Tomon

Hello

The problem is like this:

All I want is to launch a macro when user saves document. Macro should check
if a defined string is present in footer of document. If not - the macro
should open info form with possibility to enter missing string (string = id
+ date + sth... of document).
Please provide a sample of code or redirect me to appropriate page when I
could read how to realize it.

Best Regards

Dariusz Tomon
 
A

Andrew V

Assuming I read your request properly, the code below may help your efforts.
It overrides the built-in Save function. One bug in the code below is that
it crashes if the Save As dialog comes up and the user hits Cancel. But
that's what error-handling is for.

Sub FileSave()

'The following line defines the string expected in the footer
Const REQUIRED_STR = "Put defined string here"

With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range

'Check footer text (first section main footer only)
If InStr(1, .Text, REQUIRED_STR) = 0 Then

'Prompt user for footer text
Dim newStr As String
newStr = InputBox("Footer text should include '" & REQUIRED_STR
& "'. Enter new footer text:", "Enter string")

If newStr = "" Then 'User didn't input any text, so abort save
procedure
Exit Sub
Else 'Place specified text into footer
.Text = newStr
End If
End If

End With

'Save file
ActiveDocument.Save

End Sub
 

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