如何使用 C# 以编程方式将 Microsoft Word 注释转换为书签?

发布于 2024-12-01 08:52:06 字数 894 浏览 2 评论 0原文

由于书签可以包含在 URL 中,因此我想将文档中的所有注释转换为书签。

我编写了 c# 应用程序,它在 Web 浏览器 Activex 控件中显示 Microsoft Word 文档。我获得了该文档的句柄并能够枚举评论。但是当我尝试在评论位置插入书签时,我最终得到了不指向任何内容的 NULL 书签,例如:

    void ButtonConvertCommentsClick(object sender, EventArgs e)
    {
        Word.Comments wordComments = this.wordDoc.Comments;           
        MessageBox.Show("This document has " + wordComments.Count + " comments.");

        for (int n = 1; n <= wordComments.Count; n++)
        {
            Word.Comment comment = this.wordDoc.Comments[n];
            Word.Range range = comment.Range;
            String commentText = comment.Range.Text;

            this.wordDoc.Application.ActiveDocument.Bookmarks.Add("BOOKMARK"+n, range);
        }
        this.wordDoc.Save();
        ....
    }

假设文档中有 3 条评论,则显示“BOOKMARK1”、“BOOKMARK2”和“BOOKMARK3”在书签列表中,但所有这些都禁用“转到...”按钮。

我做错了什么?

Since bookmarks can be included in a URL, I want to convert all the comments in a document to bookmarks.

I wrote a c# application which displays a Microsoft Word document in a web browser activex control. I get a handle to the document and am able to enumerate the comments. But when I attempt to insert bookmarks at the comment location, I end up with NULL bookmarks that don't point to anything, e.g.:

    void ButtonConvertCommentsClick(object sender, EventArgs e)
    {
        Word.Comments wordComments = this.wordDoc.Comments;           
        MessageBox.Show("This document has " + wordComments.Count + " comments.");

        for (int n = 1; n <= wordComments.Count; n++)
        {
            Word.Comment comment = this.wordDoc.Comments[n];
            Word.Range range = comment.Range;
            String commentText = comment.Range.Text;

            this.wordDoc.Application.ActiveDocument.Bookmarks.Add("BOOKMARK"+n, range);
        }
        this.wordDoc.Save();
        ....
    }

Assuming there were 3 comments in the doc, "BOOKMARK1", "BOOKMARK2" and "BOOKMARK3" shows up in the bookmark list, but the "Go To..." button is disabled for all of them.

What am I doing wrong?

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

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

发布评论

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

评论(1

流殇 2024-12-08 08:52:06

使用 scope 获取评论的范围...

    for (int n = 1; n <= wordComments.Count; n++)
    {
        Word.Comment comment = this.wordDoc.Comments[n];
        Word.Range range = this.wordDoc.Range(comment.Scope.Start, comment.Scope.End);
        String commentText = comment.Range.Text;

        this.wordDoc.Application.ActiveDocument.Bookmarks.Add("BOOKMARK"+n, range);
    }

Use scope to get the range of the comment...

    for (int n = 1; n <= wordComments.Count; n++)
    {
        Word.Comment comment = this.wordDoc.Comments[n];
        Word.Range range = this.wordDoc.Range(comment.Scope.Start, comment.Scope.End);
        String commentText = comment.Range.Text;

        this.wordDoc.Application.ActiveDocument.Bookmarks.Add("BOOKMARK"+n, range);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文