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.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
One of the powerful programming structures provided in VBA allows you to conditionally execute commands. The If ... End ...Discover More
Do you routinely need to work with tomorrow's date? Why not create a template that automatically adds tomorrow's date to ...Discover More
Word apparently has a bug in it that makes it very difficult to remove a shortcut key previously associated with a macro. ...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.