我在Word 2007工作。我试图将合并字段插入到特定的单元格(即第二行的第一个单元格)中,但没有成功。对于简单的插入mergefield,我没有问题:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD $!test ", PreserveFormatting:=True但是当我试图将mergefield插入到单元格中时,它会抛出一个错误:
运行时错误“4605”。该命令不可用。
这就是我尝试过的代码:
collectTable.Cell(1, 1).Select
Selection.Fields.Add Selection.Range, Type:=wdFieldEmpty, Text:="MERGEFIELD $!testField", PreserveFormatting:=True我也试过这个:
collectTable.Cell(1, 1).Select
Selection.Fields.Add collectTable.Cell(1, 1).Range, Type:=wdFieldEmpty, Text:="MERGEFIELD $!testField", PreserveFormatting:=True但结果是一样的。如何使用VBA脚本在表单元格中插入mergefield?
发布于 2014-05-31 06:49:21
在我看来,当您在表中选择单元格时,所选内容包括不能用字段替换的结束单元格标记。您需要选择不包括最终标记的单元格范围。
我建议对您的代码进行以下改进:
Dim collectTable As Table
Set collectTable = ActiveDocument.Tables(1)
'selecting cell range without cell-end mark
ActiveDocument.Range(collectTable.Cell(1, 1).Range.Start, _
collectTable.Cell(1, 1).Range.End - 1).Select
Selection.Fields.Add Selection.Range, _
Type:=wdFieldEmpty, _
Text:="MERGEFIELD $!testField", _
PreserveFormatting:=Truehttps://stackoverflow.com/questions/23950418
复制相似问题