{"id":66113,"date":"2006-11-03T16:32:00","date_gmt":"2006-11-03T16:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/03\/how-can-i-run-a-macro-after-opening-up-word\/"},"modified":"2006-11-03T16:32:00","modified_gmt":"2006-11-03T16:32:00","slug":"how-can-i-run-a-macro-after-opening-up-word","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-run-a-macro-after-opening-up-word\/","title":{"rendered":"How Can I Run a Macro After Opening Up Word?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! How can I run a specified Word macro after opening Word using a script?<\/p>\n<p>&#8212; JD<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, JD. You know, when they first tested the atomic bomb a number of physicists had legitimate fears that the bomb might set off an unstoppable chain reaction that would end up destroying the entire world. We have to admit that, when we read your question we had the same sort of concern: a script that would start a macro that might then start a batch file that might then start a Windows PowerShell session that might then destroy the entire world. Not a pleasant thought, especially for those of us who have season tickets to the University of Washington men\u2019s basketball. <\/p>\n<p>On the other hand, nothing lasts forever, and we\u2019ve always assumed that when the world <i>does<\/i> end the Scripting Guys will get blamed for it anyway. So what the heck; here\u2019s a script that opens a Word document and then runs a specified Word macro:<\/p>\n<pre class=\"codeSample\">Set objWord = CreateObject(\"Word.Application\")\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Open(\"C:\\Scripts\\Test.doc\")\nobjWord.Run \"Macro1\"\n<\/pre>\n<p>Actually, we agree with you: you\u2019d think a script this dangerous and this apocalyptic would be a little more impressive, wouldn\u2019t you? For better or worse, though, it\u2019s actually pretty easy to call a macro from within a script. As you can see, we start out by creating an instance of the <b>Word.Application<\/b> object and then setting the <b>Visible<\/b> property to True; that gives us a running instance of Word that we can see on screen. We then use this line of code to open the document C:\\Scripts\\Test.doc:<\/p>\n<pre class=\"codeSample\">Set objDoc = objWord.Documents.Open(\"C:\\Scripts\\Test.doc\")\n<\/pre>\n<p>So how do we run a macro once the document is open? Like this:<\/p>\n<pre class=\"codeSample\">objWord.Run \"Macro1\"\n<\/pre>\n<p>Again, there isn\u2019t much to this: we simply call the <b>Run<\/b> method (which is part of the Word.Application object), passing as the sole parameter the name of the macro (Macro1). And while we won\u2019t go so far as to say that there\u2019s <i>nothing<\/i> easier than this, well, off the top of our heads we couldn\u2019t really come up with anything.<\/p>\n<p>Incidentally, the first time we tried this script we started by manually opening Test.doc, creating a macro named Macro1, then saving and closing the document. After the macro had been created we then used the script to reopen the document and run Macro1. Following our success there we deleted Macro1 from Test.doc, opened the Word template Normal.dot, then recreated Macro1 in the template file. After closing Normal.dot we ran the script again; once again, it worked. What does that mean? That means that this approach can be used to run a macro stored in an individual document <i>or<\/i> a macro stored in a template attached to that document.<\/p>\n<p>And because we Scripting Guys like to live dangerously we finished off the day by using the following script to create a brand-new document, type in some text, and then run the macro (which, in case you\u2019re wondering, simply selects all the text in the document and makes it boldface):<\/p>\n<pre class=\"codeSample\">Set objWord = CreateObject(\"Word.Application\")\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection\nobjSelection.TypeText(\"AAAAAA\")\nobjWord.Run \"Macro1\"\n<\/pre>\n<p>Did <i>that<\/i> work? Do you even have to ask?<\/p>\n<p>That should do the trick, JD. And don\u2019t worry: there is absolutely no chance whatsoever that running this script will bring about the end of the world. No chance.<\/p>\n<p>Just in case, though, remember this: Peter Costantini wrote today\u2019s column. That\u2019s Costantini, C-O-S \u2026.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I run a specified Word macro after opening Word using a script? &#8212; JD Hey, JD. You know, when they first tested the atomic bomb a number of physicists had legitimate fears that the bomb might set off an unstoppable chain reaction that would end up destroying the entire world. [&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,395],"class_list":["post-66113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I run a specified Word macro after opening Word using a script? &#8212; JD Hey, JD. You know, when they first tested the atomic bomb a number of physicists had legitimate fears that the bomb might set off an unstoppable chain reaction that would end up destroying the entire world. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66113","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=66113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66113\/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=66113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}