多个位置的相同书签 MS-Word

多个位置的相同书签 MS-Word

我正在使用 Excel VBA 将 Excel 数据输入到 Word 文档中。执行此操作时,我在 Word 中有许多书签。有时我需要从 Excel 中提取相同的值并将其输入到 Word 文档中的多个书签中。我可以使用一个书签而不是多个书签吗?

Dim totalCount As String
wdApp.Selection.GoTo what:=-1, Name:="Total_tickets"
totalCount = Format(shSLA.Range("E4").End(xlDown).Value)
wdApp.Selection.TypeText totalCount


Dim totalResponded As String
wdApp.Selection.GoTo what:=-1, Name:="Closed_tickets"
totalResponded = Format(shSLA.Range("M4").End(xlDown).Value)
wdApp.Selection.TypeText totalResponded


Dim totalOpen As String
wdApp.Selection.GoTo what:=-1, Name:="Pending_tickets"
totalOpen = Format(shSLA.Range("R4").End(xlDown).Value)
wdApp.Selection.TypeText totalOpen


''Reusing the same variables for different bookmarks now.
wdApp.Selection.GoTo what:=-1, Name:="Service_Request_Summary_TotaA"
wdApp.Selection.TypeText totalCount

wdApp.Selection.GoTo what:=-1, Name:="Service_Request_Summary_TotaB"
wdApp.Selection.TypeText totalCount


wdApp.Selection.GoTo what:=-1, Name:="Service_Request_Summary_Resolved"
wdApp.Selection.TypeText totalResponded


wdApp.Selection.GoTo what:=-1, Name:="Service_Request_Summary_Pending"
wdApp.Selection.TypeText totalOpen

答案1

在 Word 文档中,您可以创建一个书签,然后交叉引用书签文本。

因此,您可以在一个位置为特定值创建/设置书签,然后(而不是为同一值创建多个其他书签)在您希望同一值出现的每个其他位置插入交叉引用。在“交叉引用”对话框中,只需将“引用类型”(左上角下拉菜单)更改为“书签”,并将“插入引用”(右上角下拉菜单)更改为“书签文本”。

然后,当您更新文档中的所有字段(ctrl A 然后按 F9)时,交叉引用将更新为与您的一个书签相同的值。

相关内容