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

发布于 2024-11-24 04:58:56 字数 164 浏览 0 评论 0原文

我正在尝试使用 C# 在 Word 文档中添加书签,但它不起作用,而且我在 msdn 文档和互联网上都找不到任何帮助。这就是我正在尝试做的事情。

我正在阅读 Word 文档,然后在该文档中搜索关键字,然后将该文本转换为超链接,效果很好。现在,我想将该文本创建为书签而不是超链接。我用 C# 做这一切

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.

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#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2024-12-01 04:58:56
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)
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)
娇俏 2024-12-01 04:58:56

您需要掌握 Open XML SDK。它没有很好的记录...

我不久前发布了有关将图像插入 Word 文档的内容。根本不一样,但你永远不知道,你可能会找到一些指导来帮助你开始......

操作方法:将图像插入到 Word 文档中并使用以下命令显示它OpenXML

You need to get to grips with the Open XML SDK. It's not well documented...

I posted some significant time ago about inserting images into Word documents. Not the same thing at all, but you never know, you might find some pointers to get you started...

HowTo: Insert an image into a Word document and display it using OpenXML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文