如何使用c#将URL注释添加到word文档?

时间:2016-01-27 11:49:28

标签: c# .net ms-word comments office-interop

这是我的代码。我可以发表评论,但我想在评论中加入URL。 我怎么能这样做?

  using Microsoft.Office.Interop.Word;
  using Microsoft.Office.Core;

  private void button3_Click(object sender, EventArgs e)
    {
        // Open a doc file.
        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document document = application.Documents.Open(@"e:\temp3.docx");
        object comment = "www.google.com" + Environment.NewLine;
        object missing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Comment var = document.Comments.Add(document.Words[1], comment);
        try {
            document.Hyperlinks.Add(var.Range,ref missing,ref missing,ref missing,ref missing,ref missing);
        }
        catch (Exception ex)
        { MessageBox.Show(ex.ToString()); }
        document.Close();
        application.Quit();
    }

但结果是

my objective

And bad parameter exception in Hyperlink.add

2 个答案:

答案 0 :(得分:0)

// Insert a hyperlink to the Web page.
object oAddress = "http://www.microsoft.com";
object oRange = wrdSelection.Range;
document.Hyperlinks.Add(oRange, ref oAddress, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

答案 1 :(得分:0)

    private void button3_Click(object sender, EventArgs e)
    {
        // Open a doc file.
        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document document = application.Documents.Open(@"e:\temp3.docx");
        object comment = "www.google.com" + Environment.NewLine;
        object missing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Comment var = document.Comments.Add(document.Words[1], comment);
        try {
            document.Hyperlinks.Add(var.Range,ref comment,ref missing,ref missing,ref missing,ref missing);
        }
        catch (Exception ex)
        { MessageBox.Show(ex.ToString()); }
        document.Close();
        application.Quit();
    }

在HyperLinks.Add而不是ref评论中,我已经把参考文献丢失了。