Replacing text in a header

N

Neil Humphries

We have standardized headers in 200 documents. The user will select a subset
of the documents for a particular project. I want to be able to automate the
replacement all the placeholder text in the header with project specific
text. In each header there are 8 pieces of placeholder text and 10 pieces of
fixed text.

Since the placeholder text is consistent, I want to do a find and replace on
the placeholder text. I have an array of find/replace pairs. The code below
runs, but doesn't change the header contents.

What am I not doing or what am I doing wrong?

Sub FindandReplaceHeader()
For I = 0 To UBound(arrfindreplace, 2)
'clear find and replace fields
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
End With

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
With Selection.Find
' .Replacement.Highlight = True
.Text = arrfindreplace(0, I)
.Replacement.Text = arrfindreplace(1, I)
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next

End Sub
 
L

Lene Fredborg

It is a bit difficult to test your macro without knowing exactly how your
array looks. Have you tried stepping through the code (F8) and checking that
the values of .Text and .Replacement.Text are as they are supposed to be. I
think the problem is that the code lines:

.Text = arrfindreplace(0, i)
.Replacement.Text = arrfindreplace(1, i)

should have been:

.Text = arrfindreplace(i, 0)
.Replacement.Text = arrfindreplace(i, 1)

I made a small array and tested the macro here with that change.

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

Doug Robbins - Word MVP

I would be inclined to run a macro over all of the 200 documents that
replaced all of the placeholder text with { DOCVARIABLE } fields and then
each time a document (or series of documents) have to be customized for a
particular project, have a userform into which the user would enter the
required infomation for that project which would then be assigned to
variables in the documents and the fields would be updated so that each
document displayed the required information.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

Neil Humphries

I used a msgbox to display the array values and they are what I expected.
The array is created from a chart:
Item Placeholder Text Replace With Text
If serialized: [Serial #:] Serial#
Serial #: <nnnnn> 99999
Project name: <Insert Project Name Here> Master Test

In the msg box arrfindreplace(0, i) displayed the Placeholder Text and
arrfindreplace(1, i) displayed the corresponding Replace With Text.

I didn't realize it until you questioned it, but when the original code was
written, it broke with convention and used (column, row) format because the
array is ReDim'd using the Preserve keyword.

I set a quick watch on the
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range. It reported
a value of:

"[Client1] [Client2][Client3] Section 01 11 00
[Serial #:] <nnnnn> SUMMARY OF WORK
Project Name: <Insert Project Name Here> Page 1 of 1
Project No.: <nn.nnnnn.nn> Dated: <MMM, DD 20YY>
Consultan

I note that this is only the first part of the header, but all that displays
in the value area of the watches.

All that said, the code still doesn't replace anything.
 
N

Neil Humphries

Thanks for the suggestion, I hadn't considered using {DOCVARIABLE] fields.
I'll have to read up on them. I had considered bookmarking some of the header
information so that I could have ref's in the main story of the document
which would update with the header.

To follow your suggestion, I would still need to get a find and replace
macro working to replace my placeholder text with a field. So far, I'm no
closer to that.
 
L

Lene Fredborg

OK. Unfortunately, I don't think I am able to come up with other suggestions
right now and I will not have access to my computer in some days from now. I
hope somebody else will be able to help you. Also note Doug's reply where he
suggest another approach to the problem.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
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