{"id":70433,"date":"2005-02-15T17:02:00","date_gmt":"2005-02-15T17:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/15\/how-can-i-add-a-hyperlink-to-a-word-document\/"},"modified":"2005-02-15T17:02:00","modified_gmt":"2005-02-15T17:02:00","slug":"how-can-i-add-a-hyperlink-to-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-hyperlink-to-a-word-document\/","title":{"rendered":"How Can I Add a Hyperlink to a Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I add a hyperlink to a Word document?<BR><BR>&#8212; BA<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, BA. Interesting question; in thinking about common tasks for scripting Microsoft Office applications we never even considered adding a hyperlink to a document. (Needless to say, that\u2019s why we rely on people like you to let us know about the tasks people <I>really<\/I> want to script.) As it turns out, it\u2019s pretty easy to add a hyperlink to a Microsoft Word document; in fact, we were able to do it with just a few lines of code:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = TRUE\nSet objDoc = objWord.Documents.Add()\nSet objRange = objDoc.Range()\nSet objLink = objDoc.Hyperlinks.Add _\n    (objRange, &#8221; http:\/\/www.microsoft.com\/technet\/scriptcenter &#8220;, , , &#8220;Script Center&#8221;)\n<\/PRE>\n<P>The first three lines in this script are just boilerplate code. We start out by creating an instance of the <B>Word.Application<\/B> object. We then set the <B>Visible<\/B> property to TRUE because, as a demonstration script, we want to make sure you can see what\u2019s happening to the document. Next we create a new document, which gives us a blank piece of paper to work on. In line 4, we create an instance of the Range object; we\u2019ll use this to tell Word that we want to create the hyperlink at the current cursor location. If you\u2019ve done any scripting in Word, this should all look very familiar.<\/P>\n<P>That brings us to this line of code, which actually creates the hyperlink:<\/P><PRE class=\"codeSample\">Set objLink = objDoc.Hyperlinks.Add _\n    (objRange, &#8221; http:\/\/www.microsoft.com\/technet\/scriptcenter &#8220;, , , &#8220;Script Center&#8221;)\n<\/PRE>\n<P>What we do here is call the <B>Add<\/B> method to add a new link to the <B>Hyperlinks<\/B> collection. When we do this, we need to pass the Add method a few parameters:<\/P>\n<TABLE class=\"dataTable\" id=\"EPD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Parameter<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Description<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">objRange <\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Hyperlink \u201canchor,\u201d which indicates the spot in the document where we want to create the hyperlink. In this sample script, we\u2019re creating the hyperlink right at the start of the document. <\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">http:\/\/www.microsoft.com\/technet\/scriptcenter <\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">URL we want to link to. <\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><I>blank<\/I><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Enables us to link to a bookmark or other specified section within the HTML document. We aren\u2019t linking to a bookmark in this script, so we leave this parameter blank. <\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><I>blank<\/I><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Optional tooltip that appears when you hold the mouse over the hyperlink. We aren\u2019t using a tooltip in this sample script, so we leave this parameter blank. <\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Script Center <\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Text for the hyperlink (that is, the text you see in the document, which may or may not be the destination URL). <\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><I>blank<\/I><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Enables us to specify the window or frame in which the HTML document opens. We don\u2019t specify a target in this sample script, but if we wanted to open the Web page in a new window we could specify <B>\u201c_blank\u201d<\/B> as the target. <\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>When we finish running the script we should have a new hyperlink that looks &#8211; and functions &#8211; just like this one:<\/P>\n<P><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/default.mspx\">Script Center<\/A><\/P>\n<P>That should do the trick. Give it a try and see what happens.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a hyperlink to a Word document?&#8212; BA Hey, BA. Interesting question; in thinking about common tasks for scripting Microsoft Office applications we never even considered adding a hyperlink to a document. (Needless to say, that\u2019s why we rely on people like you to let us know about the [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5],"class_list":["post-70433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add a hyperlink to a Word document?&#8212; BA Hey, BA. Interesting question; in thinking about common tasks for scripting Microsoft Office applications we never even considered adding a hyperlink to a document. (Needless to say, that\u2019s why we rely on people like you to let us know about the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}