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