本文介绍了如何使用c#/VB.NET在单词中插入书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#在Word文档中添加书签,但是它不起作用,在msdn文档和Internet上都找不到任何帮助.这是我要尝试的方法.

I am trying to add bookmarks in word document using C# but it doesn't work and I can't find any help neither in msdn documentation nor on internet. Here is how I am trying to do.

我正在阅读Word文档,然后在该文档中搜索一个关键字,然后将该文本转换为超链接,效果很好.现在,我想将该文本创建为书签而不是超链接.我正在用C#进行所有操作

I am reading word documents and then search a keyword in that document and then I convert that text to a hyperlink and that works great. Now, I want to create that text to a bookmark instead of hyperlink. I am doing all this in 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)

这篇关于如何使用c#/VB.NET在单词中插入书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 07:08