Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > best way to insert a hyperlink into a word document using automation
Ask a questionAsk a question
 

Answerbest way to insert a hyperlink into a word document using automation

  • Tuesday, November 03, 2009 3:12 PMdaktmacfan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi,

    I am updating word documents which have DOCVARIABLE & INCLUDEPICTURE fields.

    Now I am in need of inserting a hyperlink.  So far I have been unable to do this.  I found the HYPERLINK automation field, but I was actually unable to update it in code.  How do I go about doing this?

    Here is skellton of my code:

    F is of type Fields and myR is of type Range

    for j:=1 to F.Count {
       case F.Item(j).Type_
          wdFieldIncludePicture:
              //picture code
          wdFieldIncludeText:
              if(F.Item(j).LinkFormat.SourceName == "QC")
                 F.Item(j).LinkFormat.SourceFullName=QCFilePath;
         wdFieldDocVariable:
            myR=F.Item(j).Result;
            myR.Text:="Some text";
         wdFieldHyperlink:
             if(F.Item(j).LinkFormat.SourceName == "LinkToCsv")
             {
                 //how do I update the hyperlink now
              }
         
    
    }
    
    • Edited bydaktmacfan Tuesday, November 03, 2009 3:13 PMupdated code block with formatting
    •  

Answers

  • Wednesday, November 04, 2009 6:43 AMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hyperlinks are handled as objects in the Word object model. Sample pseudocode:

    Word.Hyperlink h = oRange.Hyperlinks.Add( //parameters
    h.Address = "URL path"

    and so forth

    Please note that you need to be patient when posting. These forums are a service, partly volunteer-supported. You may need to wait for a couple of days before getting a response. If you need faster answers, please use paid support channels.


    Cindy Meister, VSTO/Word MVP
    • Marked As Answer bydaktmacfan Wednesday, November 04, 2009 8:49 PM
    •  

All Replies

  • Tuesday, November 03, 2009 8:23 PMdaktmacfan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Nobody has any thoughts here?

  • Wednesday, November 04, 2009 6:43 AMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hyperlinks are handled as objects in the Word object model. Sample pseudocode:

    Word.Hyperlink h = oRange.Hyperlinks.Add( //parameters
    h.Address = "URL path"

    and so forth

    Please note that you need to be patient when posting. These forums are a service, partly volunteer-supported. You may need to wait for a couple of days before getting a response. If you need faster answers, please use paid support channels.


    Cindy Meister, VSTO/Word MVP
    • Marked As Answer bydaktmacfan Wednesday, November 04, 2009 8:49 PM
    •  
  • Wednesday, November 04, 2009 8:49 PMdaktmacfan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for your help/insight Cindy.

    Sorry for my impatientce.  I was under a time crunch and office automation is very foreign to me.  Most of the other MSDN forums I am participated in the past you get a reply within 30 min.

    Thank you for sharing your expertese.