Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#向word文档添加URL注释?_C#_.net_Ms Word_Comments_Office Interop - Fatal编程技术网

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

如何使用c#向word文档添加URL注释?,c#,.net,ms-word,comments,office-interop,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 appl

这是我的密码。我可以放评论,但我想把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();
    }
但结果是这样的


在超链接中。添加而不是引用注释。我想在注释中插入URL,但您的解决方案是在文档文本中插入URL。选中此项@Isuru您的链接不相关,因为它使用的是开放式XML SDK,而不是Word对象模型。然而,您的总体方法是正确的。OP只需将正确的范围传递给该方法。@Pouriagolshanrad。你有你需要的所有信息,你只需要把它们组合起来。Add方法返回一个Comments对象,因此为其分配一个变量。Comment对象有一个Range属性,然后将其传递给超链接。添加Isuru提供给您的属性,以代替oRange。
// 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);
    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();
    }