{"id":70803,"date":"2004-12-15T10:20:00","date_gmt":"2004-12-15T10:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/15\/how-can-i-grab-a-url-from-the-clipboard-and-then-open-that-web-site-in-a-browser\/"},"modified":"2004-12-15T10:20:00","modified_gmt":"2004-12-15T10:20:00","slug":"how-can-i-grab-a-url-from-the-clipboard-and-then-open-that-web-site-in-a-browser","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-grab-a-url-from-the-clipboard-and-then-open-that-web-site-in-a-browser\/","title":{"rendered":"How Can I Grab a URL From the Clipboard and Then Open That Web Site in a Browser?"},"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 grab a URL from the clipboard and then open that Web site in a browser?<BR><BR>&#8212; CL<\/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, CL. Interesting question, or, should we say, <I>questions<\/I>. That\u2019s because there\u2019s really two issues here. The first one is easy: can I use a script to open a specified Web site? As you probably know, the answer to that one is a resounding yes. Here\u2019s a sample script that stores the Script Center\u2019s URL in a variable named strURL. The script then creates an instance of the WSH Shell object, and uses the Run method to open up your default Web browser and navigate to the specified URL:<\/P><PRE class=\"codeSample\">strURL = \u201chttp:\/\/www.microsoft.com\/technet\/scriptcenter\/default.mspx\u201d\nSet objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run(strURL)\n<\/PRE>\n<P>The second question is a bit trickier: can I grab information off the clipboard using a script? It turns out that the answer to this one is also yes, although you have to get at the clipboard through the backdoor. <\/P>\n<P>Neither WSH nor VBScript are able to interact with the clipboard: neither one allows you to copy data to the clipboard and or to paste data from the clipboard. Internet Explorer, on the other hand, <I>can<\/I> interact with the clipboard. (Hey, Internet Explorer can do <I>anything<\/I>!) So, we\u2019ll just let IE do the work for us. If you want to grab data from the clipboard, you can use code similar to this:<\/P><PRE class=\"codeSample\">Set objIE = CreateObject(&#8220;InternetExplorer.Application&#8221;)\nobjIE.Navigate(&#8220;about:blank&#8221;)\nstrURL = objIE.document.parentwindow.clipboardData.GetData(&#8220;text&#8221;)\nobjIE.Quit\nWscript.Echo strURL\n<\/PRE>\n<P>What we\u2019re doing here is creating an instance of Internet Explorer and opening it to a blank page. Note that you won\u2019t actually <I>see<\/I> this instance of IE; that\u2019s because we didn\u2019t set the Visible property to TRUE. Instead, everything happens in the background.<\/P>\n<P>We then use the clipboardData.GetData method to get the text that happens to be sitting on the clipboard and store it in the variable strURL; that\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">strURL = objIE.document.parentwindow.clipboardData.GetData(&#8220;text&#8221;)\n<\/PRE>\n<P>We dismiss this instance of IE (objIE.Quit) and then echo the value that we retrieved from the clipboard.<\/P>\n<P>Give this a try: copy some text to the clipboard and then run the script. You should get back a message box consisting of that text you just copied to the clipboard.<\/P>\n<P>Now it\u2019s just a matter of putting the two script halves together to make a whole. Here\u2019s a script that grabs a URL from the clipboard and then opens that Web site in your default Web browser:<\/P><PRE class=\"codeSample\">Set objIE = CreateObject(&#8220;InternetExplorer.Application&#8221;)\nobjIE.Navigate(&#8220;about:blank&#8221;)\nstrURL = objIE.document.parentwindow.clipboardData.GetData(&#8220;text&#8221;)\nobjIE.Quit<\/p>\n<p>Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run(strURL)\n<\/PRE>\n<P>So maybe that wasn\u2019t so bad after all. And as an added bonus, this script doesn\u2019t limit you to just opening Web sites. Suppose you have a file path like <B>C:\\Scripts\\ScriptLog.txt<\/B> on the clipboard. Run the script, and the file will open up in Notepad (or whatever application you have associated with .txt files). If you have the path to a .doc file on the clipboard, the script will open that document in Microsoft Word. It\u2019s really more of a general purpose file opening script than it is a Web site-only opening script.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I grab a URL from the clipboard and then open that Web site in a browser?&#8212; CL Hey, CL. Interesting question, or, should we say, questions. That\u2019s because there\u2019s really two issues here. The first one is easy: can I use a script to open a specified Web site? As [&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":[17,3,167,5],"class_list":["post-70803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-internet-explorer","tag-scripting-guy","tag-using-the-internet","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I grab a URL from the clipboard and then open that Web site in a browser?&#8212; CL Hey, CL. Interesting question, or, should we say, questions. That\u2019s because there\u2019s really two issues here. The first one is easy: can I use a script to open a specified Web site? As [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70803","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=70803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70803\/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=70803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}