如何使用c#/VB.NET在word中插入书签

本文关键字:word 插入 书签 NET 何使用 VB | 更新日期: 2023-09-27 18:02:44

我正在尝试使用c#在word文档中添加书签,但它不起作用,我在msdn文档和互联网上都找不到任何帮助。以下是我正在尝试做的。

我正在阅读word文档,然后在该文档中搜索关键字,然后我将该文本转换为超链接,这很有效。现在,我想创建一个书签文本,而不是超链接。这些都是用c#写的

如何使用c#/VB.NET在word中插入书签

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文档中插入图像的文章。完全不是一回事,但你永远不知道,你可能会找到一些指针来帮助你开始…

如何将图像插入Word文档并使用OpenXML显示