Replace footer text

S

Steve C

I need my code to be able to search a footer section for the text "<Project
#>" in a multiple page, single section document and replace it with the value
of a variable (ProjNum) provided from a user form. Can someone help me out?
Thanks!
 
L

Lene Fredborg

You will find a macro (function) below that should do the job. Some comments
first:

1. Three different footers exist: first page footer, primary footer, even
page footer. In the macro, I have assumed your footer is a "primary footer" –
it is if the document does not have "Different first page" and "Different odd
and even" turned on in File > Page Setup > Layout tab.

2. In a document with more than one section, the macro will only replace
text in the footer of section 1.

3. The function has a parameter, sReplacement. When calling the function,
sReplacement must be set to the value of ProjNum (supposed to be a string).
You can use the following line of code to call the function:

ReplaceTextInFooter sReplacement:=ProjNum

This will execute the function below. The function can be placed in any
module.

Function ReplaceTextInFooter(sReplacement As String)
Dim oRange As Range

Set oRange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
With oRange.Find
.Text = "<Project #>"
.Replacement.Text = sReplacement
.Execute Replace:=wdReplaceAll
End With

Set oRange = Nothing
End Function

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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