{"id":66263,"date":"2006-10-13T12:07:00","date_gmt":"2006-10-13T12:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/13\/how-can-i-change-an-existing-hyperlink-in-a-microsoft-word-document\/"},"modified":"2006-10-13T12:07:00","modified_gmt":"2006-10-13T12:07:00","slug":"how-can-i-change-an-existing-hyperlink-in-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-an-existing-hyperlink-in-a-microsoft-word-document\/","title":{"rendered":"How Can I Change an Existing Hyperlink in a Microsoft Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I change an existing hyperlink in a Microsoft Word document?<BR><BR>&#8212; PH<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, PH. You know, we\u2019re impressed that you had the guts to ask a question today; after all, today <I>is<\/I> Friday the 13<SUP>th<\/SUP>, a date many people dread, fearing that anything bad that <I>could<\/I> happen <I>will<\/I> happen. For example, people who suffer from Paraskevidekatriaphobia (a debilitating fear of Friday the 13<SUP>th<\/SUP>) would <I>never<\/I> ask the Scripting Guys a question on this day: they\u2019d be too afraid that we\u2019d give them a script that didn\u2019t work, that we\u2019d write a column that didn\u2019t make sense, or that we\u2019d blabber on and on about some arcane trivia that has nothing to do with scripting or with the question they asked.<\/P>\n<P>Superstitious nonsense. Bah. Humbug!<\/P>\n<TABLE id=\"EPD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Trivia note<\/B>. Fridays have long been considered unlucky; according to tradition, Adam and Eve took their fateful bite of the apple on a Friday. (Although we should note that Peter Costantini, the oldest living Scripting Guy, says he remembers the two biting into the apple on a Wednesday morning.) Likewise, the number 13 has long been considered an unlucky number; many ancient cultures believed that it was unlucky to invite 13 people to dinner. (Which would definitely be true if you were the one <I>cooking<\/I> the dinner.) However, superstitions involving Friday the 13<SUP>th<\/SUP> seem to be a modern phenomenon; no references to Paraskevidekatriaphobia (or anything simlar) can be found prior to the 20<SUP>th<\/SUP> century.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But, like we said, this is all superstitious nonsense. As it turns out, PH, this is your <I>lucky<\/I> day; after all, we just happened to have a script that can change an existing link in a Microsoft Word document:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;c:\\scripts\\test.doc&#8221;)<\/p>\n<p>Set colHyperlinks = objDoc.Hyperlinks<\/p>\n<p>For Each objHyperlink in colHyperlinks\n    If objHyperlink.Address = &#8220;http:\/\/www.microsoft.com\/&#8221; Then                                \n        objHyperlink.Address = &#8220;http:\/\/www.microsoft.com\/technet\/scriptcenter\/&#8221;\n    End If\nNext\n<\/PRE>\n<P>As you can see, we start out by creating an instance of the <B>Word.Application<\/B> object; we then set the <B>Visible<\/B> property to True, giving us a running instance of Microsoft Word that we can see onscreen. (Of course, you don\u2019t <I>have<\/I> to see Word onscreen in order to change a hyperlink. For educational purposes, however, we\u2019ve always found it better if people can watch as the excitement unfolds.)<\/P>\n<P>With Word up and running we then use the <B>Open<\/B> method to open the document C:\\Scripts\\Test.doc. As it turns out, Test.doc happens to have several hyperlinks pointing to <B>http:\/\/www.microsoft.com\/<\/B>. That, of course, is the Microsoft.com home page; why the heck would anyone want to go <I>there<\/I>? Consequently, we need our script to locate all instances of this particular hyperlink and change the target address to the Internet\u2019s true hot spot: <B>http:\/\/www.microsoft.com\/technet\/scriptcenter\/<\/B>.<\/P>\n<TABLE id=\"ECF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. No, Google has not offered us $1.6 billion for the Script Center, at least not yet. But that\u2019s probably because they know that the Scripting Guys would never sell.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how do we go about changing these hyperlinks? Well, to begin with, we use this line of code to retrieve a collection of all the hyperlinks in the document:<\/P><PRE class=\"codeSample\">Set colHyperlinks = objDoc.Hyperlinks\n<\/PRE>\n<P>Once we have the collection in hand we set up a For Each loop to loop through each hyperlink in the collection. Inside that loop we use the following line of code to see if the <B>Address<\/B> property (the property representing the hyperlink target) is equal to <I>http:\/\/www.microsoft.com\/<\/I>:<\/P><PRE class=\"codeSample\">If objHyperlink.Address = &#8220;http:\/\/www.microsoft.com\/&#8221; Then\n<\/PRE>\n<P>If Address <I>isn\u2019t<\/I> equal to <I>http:\/\/www.microsoft.com\/<\/I> we skip that hyperlink, loop around, and continue the process with the next item in the collection. If the hyperlink <I>is <\/I>equal to <I>http:\/\/www.microsoft.com\/<\/I>, we then use this line of code to retarget the link to the Script Center instead:<\/P><PRE class=\"codeSample\">objHyperlink.Address = &#8220;http:\/\/www.microsoft.com\/technet\/scriptcenter\/&#8221;\n<\/PRE>\n<P>And then we loop around and check the next link in the collection. When we\u2019re all done all the links that used to point to the Microsoft.com home page will now point to the Script Center. (You can verify that by holding the mouse over each link and viewing the little target address popup that will appear.)<\/P>\n<P>There you have it, PH. Best of all, it turns out that this is an even <I>luckier<\/I> day for you than we first thought. Not only did we manage to answer your question, but we did it quickly enough that we have time to relate even <I>more<\/I> historical facts for you. For example, you\u2019re familiar with Noah\u2019s Ark, right? According to legend, the rain began falling on a Friday. The destruction of the Tower of Babel? Occurred on a Friday. And then here\u2019s the &#8212;<\/P>\n<TABLE id=\"EQG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Sorry, PH; it seems as though Friday the 13<SUP>th <\/SUP>really <I>is<\/I> an unlucky day. After all, we had several more pages of fascinating Friday facts, but the Scripting Editor seems to have inadvertently deleted them. Please accept our deepest apologies. We\u2019ll see what we can do to retrieve all that information for you. It just wouldn\u2019t be a <I>Hey, Scripting Guy!<\/I> without it.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change an existing hyperlink in a Microsoft Word document?&#8212; PH Hey, PH. You know, we\u2019re impressed that you had the guts to ask a question today; after all, today is Friday the 13th, a date many people dread, fearing that anything bad that could happen will happen. For example, [&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-66263","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 change an existing hyperlink in a Microsoft Word document?&#8212; PH Hey, PH. You know, we\u2019re impressed that you had the guts to ask a question today; after all, today is Friday the 13th, a date many people dread, fearing that anything bad that could happen will happen. For example, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66263","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=66263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66263\/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=66263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}