Replacing Text with a Field in Footers

A

Andy

Hi,
I am writing a macro that operates after a mail merge has
executed and has changed all the field codes to text. I
need to be able to search all of the footers (in my case
there are FirstPage footers and sometimes Primary footers)
and replace a "marker" (such as "<<FileName>>") with the
FileName field. Since the document results from a merge,
there are multiple Sections of a variable number. I also
need to make sure that the field is formatted in a 9 pt.
font. I have written language which works with one
section but not with a multiple section documant. Any
help would be appreciated. Thanks.

Andy
 
D

Doug Robbins - Word MVP

Hi Andy,

If you can live with the filename being inserted as the first item in the
footer, it's a lot easier than locating and replacing a marker.

Dim i As Integer, myrange As Range
For i = 1 To ActiveDocument.Sections.Count
Set myrange =
ActiveDocument.Sections(i).Footers(wdHeaderFooterFirstPage).Range
myrange.End = myrange.Start
myrange.Fields.Add Range:=myrange, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Set myrange =
ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range
myrange.End = myrange.Start
myrange.Fields.Add Range:=myrange, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Next i

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
A

Andy

Doug,

Thanks for the routine. It is helpful though I would
prefer the locating and replacing technique (if possible)
since I want to include the technique "inside" a
DocumentBefore Save Event. The advantage of the replacing
technique is that if a document is saved, modified and
resaved, I believe your technique will add the filename
field a second time rather than simply not finding a
marker to act on. I could use your technique; however, I
would then have to either ask the user to tell me if the
field should be added or somehow determine if the document
has ever been saved under any filename before and the
FileName field already added (this seems very difficult).
Is the locating and replacing technique feasible?

By the way, for anyone else reading this, I shortened your
suggestion slightly to read as follows:

Dim i As Integer
Dim myrange As Range
For i = 1 To ActiveDocument.Sections.Count
Set myrange = ActiveDocument.Sections(i).Footers
(wdHeaderFooterFirstPage).Range
myrange.End = myrange.Start
myrange.Fields.Add Range:=myrange,
Type:=wdFieldFileName, PreserveFormatting:=True
Set myrange = ActiveDocument.Sections(i).Footers
(wdHeaderFooterPrimary).Range
myrange.End = myrange.Start
myrange.Fields.Add Range:=myrange,
Type:=wdFieldFileName, PreserveFormatting:=True
Next i

Thanks,
Andy
 
D

Doug Robbins - Word MVP

Hi Andy,

If you want to go the Edit>Replace route, see the article "Using a macro to
replace text where ever it appears in a document including Headers, Footers,
Textboxes, etc." at:

http://www.mvps.org/word/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
A

Andy

Doug,

Sorry to be a bother (I hope not), but your article did
not cover the one problem I had which started this whole
question. I couldn't see how to use the Find/Replace
route when I was trying to find text and replace it with a
field code as opposed to new text. What I need to be able
to do (I assumed) is find the text, delete it, insert the
field, format it if necessary and then find the next
occurrence in the next footer, etc.

Is this possible?

Andy
 
D

Doug Robbins - Word MVP

Andy,

Use the following, (which may need some tweaking), in conjunction with the
information in that article:

Dim myrange As Range
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<<File Name>>", Wrap:=wdFindContinue,
Forward:=True) = True
Set myrange = Selection.Range
myrange.Fields.Add Range:=myrange, Type:=wdFieldFileName,
PreserveFormatting:=True
Loop
End With

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
G

Guest

Doug,

Unfortunately, the article uses StoryRanges and your
suggestion includes the Add method which apparently isn't
available (Word VBA Help on the StoryRanges Collection
Object). I tried the following somewhat inelegant method
which worked for the first Section but didn't do the
others:

Dim myrange As Range
Dim sect As Section

For Each sect In ActiveDocument.Sections

With sect.Footers(wdHeaderFooterFirstPage)
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<<FileName>>",
Wrap:=wdFindContinue, Forward:=True) = True
Set myrange = Selection.Range
myrange.Fields.Add Range:=myrange,
Type:=wdFieldFileName, PreserveFormatting:=True
Loop
End With
End With

With sect.Footers(wdHeaderFooterPrimary)
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<<FileName>>",
Wrap:=wdFindContinue, Forward:=True) = True
Set myrange = Selection.Range
myrange.Fields.Add Range:=myrange,
Type:=wdFieldFileName, PreserveFormatting:=True
Loop
End With
End With

Next sect

Can this be modified to do each section?

Andy
 
A

Andy

Doug,

I noticed in my last response that I did not include my
name in the Sender's Name field

Sorry.

Andy
 

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