开发者

How can I programmatically convert Microsoft Word comments to bookmarks using C#?

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?


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜