by Allen Wyatt
(last updated April 19, 2014)
Grace is creating a Word macro that performs a mail merge. She wants to start Word from the command line and have it run this macro, which she knows how to do. What she is trying to figure out is how, on the same command line, to pass a data file name that can be accessed by the macro for subsequent use.
A command entered on the operating system's command line can include parameters; this happens all the time. Those parameters can then be accessed by the coding of the command being executed. The problem is that when you start Word on the command line, any parameters are used (or attempted to be used) by Word itself. You need a way for Word to recognize that those parameters are for a macro and then pass them on to the macro. This "recognize and pass on" behavior is not one that is built into Word.
That being said, there are some things you can do to get around this limitation, but the all involve working outside of Word, at a Windows API level. This is pretty advanced stuff, and some suggestions for how to go about it can be found at these sites:
A simpler approach might be to simply make some changes to the macro that runs when Word is started. The macro could, for instance, display an inputbox to request the user to enter a file name. Similarly, the macro could display an Open dialog box and allow the user to select the file to be processed. Finally, if the file name is standardized (meaning it is always the same and doesn't change), you could directly code the file name into the macro.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13068) applies to Microsoft Word 2007, 2010, and 2013.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
If you are applying formatting from within a macro, you may want to change the alignment of various paragraphs. Here's ...Discover More
Sometimes it can come in handy to know who the current computer user is, as far as Word is concerned. This tip presents ...Discover More
Need to format your document using a macro? You can easily set the right margin for an individual paragraph by using the ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.