0

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
4

2 回答 2

0

Instr 在涉及表、字段等的地方不可靠。此外,在您的代码中,Selection.Range 指的是Excel选择!要引用 Word 选择,您需要 wd.Selection.Range。在任何情况下都不需要选择任何东西。尝试:

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
于 2019-05-13T00:27:58.987 回答
0

道歉使它更清楚。在文档标题中有一个包含 3 个单元格的表格(在右侧)。第二个有硬编码地址,例如 1 Maple Road SomeTown SomeCity SomePostCode 我需要用合并字段替换此单元格的内容,例如
MERGEFIELD Address_Line1
MERGEFIELD Address_Line2
MERGEFIELD Address_City MERGEFIELD Address_PostCode
(只要硬编码条目与指定的 Road、Town、City 和 PostCode 匹配) 这是在 excel VBA 中完成的批处理作业,一次针对一个文件夹,其中包含要更新的多个文档。格式也需要保留 谢谢

于 2019-05-16T09:50:00.843 回答