How Can I Run a Macro After Opening Up Word?
Hey, Scripting Guy! How can I run a specified Word macro after opening Word using a script?
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’s basketball.
On the other hand, nothing lasts forever, and we’ve always assumed that when the world does end the Scripting Guys will get blamed for it anyway. So what the heck; here’s a script that opens a Word document and then runs a specified Word macro:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc") objWord.Run "Macro1"
Actually, we agree with you: you’d think a script this dangerous and this apocalyptic would be a little more impressive, wouldn’t you? For better or worse, though, it’s actually pretty easy to call a macro from within a script. As you can see, we start out by creating an instance of the Word.Application object and then setting the Visible 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:
Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
So how do we run a macro once the document is open? Like this:
Again, there isn’t much to this: we simply call the Run method (which is part of the Word.Application object), passing as the sole parameter the name of the macro (Macro1). And while we won’t go so far as to say that there’s nothing easier than this, well, off the top of our heads we couldn’t really come up with anything.
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 or a macro stored in a template attached to that document.
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’re wondering, simply selects all the text in the document and makes it boldface):
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.TypeText("AAAAAA") objWord.Run "Macro1"
Did that work? Do you even have to ask?
That should do the trick, JD. And don’t worry: there is absolutely no chance whatsoever that running this script will bring about the end of the world. No chance.
Just in case, though, remember this: Peter Costantini wrote today’s column. That’s Costantini, C-O-S ….