andythpa
andythpa

Reputation: 21

Word Document - Replace text in Header Table with Mergefields

I have a set of documents (lots) where the header in each has a table in the header with hardcoded address entries. I need to update all these documents to replace these hard-coded address with mergefields. The code is in an excel spreadsheet where the user selects the folder containing the documents to update. Below is an extract of where the updating is done e.g. trying to replace hardcoded value of 1 Maple Road with {MERGEFIELD Address_Line1}. Not sure where i'm going wrong but message is usually wrong number of arguments or does not work at all Thanks

    Dim doc As Word.Document
Dim hf As Word.HeaderFooter
Dim lr As ListRow
Dim updated As Boolean
Dim tableCount As Integer
Dim t As Integer
Dim c As Cell

Set wd = New Word.Application
Set doc = wd.Documents.Open(Filename:="c:/......./example.docx", ReadOnly:=False)


For Each hf In doc.Sections(1).Headers()

    tableCount = hf.Range.Tables.Count
    For t = 1 To tableCount
        For Each c In hf.Range.Tables(t).Range.Cells
            If InStr(1, c.Range.Text, "1 Maple Road") > 0 Then
                c.Range.Text = ""
                c.Range.Select
                doc.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=True, Text:="MERGEFIELD Address_line1"
            End If
        Next c
    Next t
Next hf


doc.Close False
wd.Quit False

Or tried

Dim doc As Word.Document
Dim hf As Word.HeaderFooter
Dim lr As ListRow
Dim updated As Boolean
Dim tableCount As Integer
Dim t As Integer
Dim c As Cell

Set wd = New Word.Application
Set doc = wd.Documents.Open(Filename:="c:/......./example.docx", ReadOnly:=False)


For Each hf In doc.Sections(1).Headers()

    tableCount = hf.Range.Tables.Count
    For t = 1 To tableCount
        For Each c In hf.Range.Tables(t).Range.Cells
            If InStr(1, c.Range.Text, "1 Maple Road") > 0 Then
                c.Range.Text = ""
                c.Range.Select
                Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=True
                Selection.TypeText Text:="MERGEFIELD Address_Line1"
            End If
        Next c
    Next t
Next hf


doc.Close False
wd.Quit False

Upvotes: 0

Views: 222

Answers (2)

andythpa
andythpa

Reputation: 21

Apologies making it clearer. In the document header there is a table (on right hand side) with 3 cells. The second on has hardcoded addresses e.g. 1 Maple Road SomeTown SomeCity SomePostCode I need to replace the contents of this cell with mergefields e.g.
MERGEFIELD Address_Line1
MERGEFIELD Address_Line2
MERGEFIELD Address_City MERGEFIELD Address_PostCode
(so long as the hardcoded entries match the specified Road,Town, City and PostCode) The is a batch job done in excel VBA that targets one folder at a time containing a number of documents to update. The formatting also needs to be retained Thanks

Upvotes: 0

macropod
macropod

Reputation: 13515

Instr is not reliable where tables, fields, and so on are involved. Furthermore, in your code, Selection.Range is referring to an Excel selection! To refer to a Word selection you'd need wd.Selection.Range. In any event there is no need to select anything. Try:

For Each hf In doc.Sections(1).Headers
  With hf.Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "1 Maple Road"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    If .Find.Found = True Then
      .Fields.Add .Range, wdFieldEmpty, "MERGEFIELD Address_line1", False
    End If
  End With
Next

Upvotes: 0

Related Questions