[英]how to insert bookmark in word using c#/VB.NET
我正在尝试使用 C# 在 word 文档中添加书签,但它不起作用,我在 msdn 文档和互联网上都找不到任何帮助。 这是我正在尝试做的事情。
我正在阅读word文档,然后在该文档中搜索关键字,然后将该文本转换为超链接,效果很好。 现在,我想将该文本创建到书签而不是超链接。 我在 C# 做这一切
Dim _wordApp As ApplicationClass
Dim _doc As Document
Dim pos, len As Integer
Dim reg As Regex
Dim bookmarkName As String
Dim rng As Object
Dim search As String = "Some Text to search"
Dim nullobj As Object = System.Reflection.Missing.Value
Dim isReadOnly As Object = False
_wordApp = New ApplicationClass()
'open the word document
_doc = _wordApp.Documents.Open(fileName, isReadOnly, nullobj, nullobj, nullobj, _
nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, _
nullobj, nullobj, nullobj, nullobj, nullobj)
_wordApp.Visible = False
' keyword that you want to search
reg = New Regex(search)
' find the text in word file
Dim m As Match = reg.Match(_doc.Range.Text, 0)
pos = m.Index
' start is the starting position of the token in the content...
len = search.Length
' select the text
rng = _doc.Range(pos, len + pos)
bookmarkName = "MyBookmarkName"
_doc.Bookmarks.Add(bookmarkName, rng)
您需要掌握 Open XML SDK。 它没有很好的记录......
很久以前,我发布了有关将图像插入 Word 文档的信息。 根本不是一回事,但你永远不知道,你可能会找到一些让你开始的指针......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.