如何以编程方式更改单词书签的文本 [英] How to change programmatically the text of a Word Bookmark
问题描述
我需要处理Word文档并更改书签的文本.
I need to process a Word document and change (the text of) a bookmark.
我将Word文档导出为平面xml格式,以帮助建立文档的结构-这是一个片段
I exported my Word document to flat xml format to help establish the structure of the document - here is a fragment
<w:bookmarkStart w:id="0" w:name="CustomerName"/>
<w:r w:rsidRPr="001E4487">
<w:rPr>
<w:rFonts w:ascii="MyTypeRegular"
w:hAnsi="MyTypeRegular"
w:cs="MyType V2 Regular"/>
<w:szCs w:val="22"/>
</w:rPr>
<w:t>[CustomerName]</w:t>
</w:r>
<w:bookmarkEnd w:id="0"/>
我需要更改的位是<w:t>[CustomerName]</w:t>
,然后应变为,例如<w:t>Some Punter</w:t>
The bit I need to change is <w:t>[CustomerName]</w:t>
which should then become, for example <w:t>Some Punter</w:t>
所以在我的VBA中,我希望能够做这样的事情...
so in my VBA i am hoping to be able to do something like this ...
Dim bkm As Bookmark
For Each bkm In ActiveDocument.Bookmarks
bkm.Text = "Some Punter"
Next bkm
仅BookMark
没有Text
属性
如何访问方括号中的那个小数据项?
How do Access that little data item in the square Brackets?
推荐答案
单词书签没有text属性,但是它的range属性却有.
A word bookmark doesn't have a text property, but its range property does.
使用bkm.Range.Text =有些赌徒"
Use bkm.Range.Text = "Some Punter"
请注意,一旦更改了文本,书签将从Word文档中删除.为了保留书签,您需要执行以下操作:
Note that once you change the text, the bookmark will be removed from the Word Document. In order to keep the bookmark you need to do:
Dim bkm As Bookmark
Dim bkmName As String
Dim bkmRng As Range
Dim NewText As String
NewText = "Some Punter"
For Each bkm In ActiveDocument.Bookmarks
Set bkmRng = bkm.Range
bkmName = bkm.Name
bkm.Range.Text = NewText
bkmRng.End = bkmRng.Start + Len(NewText)
Bookmarks.Add(Name:=bkmName, Range:=bkmRng)
Next bkm
希望这会有所帮助.
这篇关于如何以编程方式更改单词书签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!