Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and replace in header and footer 2

Status
Not open for further replies.

pluz

Instructor
Jan 26, 2004
20
US
I have a document that has placeholder text in the headers and footers of a Word document. I want to find and replace the placeholder text with actual text.

Problem: Word 2003 does not search headers/footers when doing a find/replace.

Is it possible to do a find/replace for specific text, i.e., *Tname* in all headers/footers in the document and replace it with other text, i.e., CONNER FAMILY TRUST, programatically? I tried this on my own after searching the forum(s), but have not been successful.

Any help with a macro will be greatly appreciated.

Thanks much!!
 
Hi Pluz - very quickly it will be something like:


Sub FormatFooters()

Dim myDoc As Document
Dim mySection As Section
Dim myFooter As HeaderFooter
Dim FormatFootersCounter As Byte

On Error Resume Next

Set myDoc = ActiveDocument

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If

With myDoc
For Each mySection In ActiveDocument.Sections
For Each myFooter In mySection.Footers
myFooter.Range.Select
With Selection.Find
.Text = "TName"
.Replacement.Text = "Conner Family Trust"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next myFooter
Next mySection
End With

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument


End Sub


Will need testing, but I hope this helps

Asjeff
 
This is strange because Word 2000 does search headers.

If I were setting up a new document from scratch I would use fields. For example, document properties has SUBJECT and TITLE fields that can be accessed through Insert, Field. This should work anywhere in a document.
 
From 2002 onward, Word is made up of Stories (wdMainStory, wdHeaderFooter, wdComments etc). You have to entrer that story for the Find object to do its thing.

The above code should be able to be tweaked to work, as it loops through each footer, in each section.

Gerry
 
From 2002 onward, Word is made up of Stories (wdMainStory, wdHeaderFooter, wdComments etc). You have to entrer that story for the Find object to do its thing.

I have Word 2002, and I regularly replace text in the header of one particular sort of document. I was just continuing a method I'd worked out for Word 97. But I'd be interested to know what it is that lets it work for me.

Madawc Williams (East Anglia)
 
Thank you, Asjeff. This worked great!

I created a document that pops up a UserForm that the user will type information into. This information is used to replace specific text in all open documents. To fix the header/footer find and replace issue, I saved your sample code as a separate macro and added a run macro code to run it from the UserForm. It works perfectly with your sample macro exchanging text for text, but now I need to find out how to make it insert a textbox entry from the UserForm to use as the replacement text.

For Each mySection In ActiveDocument.Sections
For Each myFooter In mySection.Footers
myFooter.Range.Select
With Selection.Find
.Text = "TName"
.Replacement.Text = tbTname.Text
[tbName = text box item]

Can this be done? Please advise and thank you all for your help with this!!
-Pat
 
If the UserForm is running, and it sounds like it is, then
Code:
For Each mySection In ActiveDocument.Sections
     For Each myFooter In mySection.Footers
       myFooter.Range.Select
       With Selection.Find
        .Text = "TName"
        .Replacement.Text = [i]formname[/i].tbTname.Text
should work.

Gerry
 
Oh, and if you are using

Set myDoc = ActiveDocument

you should make sure you release that object at the end of your routine, with a

Set myDoc = Nothing


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top