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

时间:2011-08-24 17:39:00

标签: c# ms-word bookmarks

由于书签可以包含在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”显示在书签列表中,但“Go To ...”按钮对所有文档都禁用。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用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);
    }