locked
Inserting hyperlink programmatically into Word document using C#.NET RRS feed

  • 问题

  • Hello,

    I have problems inserting hyperlinks using c# and the word/office API into a document.

    I have a list of bookmarks in a document and I want to create an appendix table with the names of the bookmarks being hyperlinks to the bookmark range in the document.
    I also have problems just creating hyperlinks in normal text.

    I'm doing this:
    doc.Hyperlinks.Add(anchor, ref missing,
                                ref subaddress, ref missing, ref missing, ref missing);

    I get the error: System.Runtime.InteropServices.COMException: Command failed at Microsoft.Office.Interop.Word.Hyperlinks.Add(Object Anchor, Object& Address, Object& SubAddress, Object& ScreenTip, Object& TextToDisplay, Object& Target)
    doc.Hyperlinks.Add(anchor, ref missing, ref subaddress, ref missing, ref missing, ref missing);

    All parameters except anchor are optional.
    The subaddress is a bookmark:
    object subaddress = FindBookmark(doc.Bookmarks, com);
    I've have also tried with the name of the bookmark, Bookmark.Name property, and using missing instead of subaddress; same error.

    Does anyone have examples of creating hyperlinks?  Either into text or tables.


    I'm using C# in Visual Studio 2005 and Word 2003.

    Thanks,

    Jens
    2006年2月28日 12:31

答案


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

    -= Maarten =-

    2006年2月28日 15:00

全部回复


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

    -= Maarten =-

    2006年2月28日 15:00

  • Thanks a lot!

    The msdn documention gives the wrong (or too little detail) on the parameters.

    Groetjes,

    Jens
    2006年2月28日 15:35
  • Where is the FindBookmark() function?
    Ahmed Mostafa
    2009年12月8日 21:23
  • The Word object model has no such function. This is something that Jens wrote.
    Cindy Meister, VSTO/Word MVP
    2009年12月9日 9:30
  • Easiest way to insert hyperlink to word document, name Spire.Doc, I use it long time.

    http://www.e-iceblue.com/Introduce/word-for-net-introduce.html

    2010年7月20日 3:59
  • Thanks! Just trying to add a link to an existing paragraph and this helped!
    2020年7月7日 18:57