Passing a Data File Name via Command Line to a Macro

Written by Allen Wyatt (last updated March 14, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


3

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 they 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:

http://vb.mvps.org/samples/CmdLine/
http://msdn.microsoft.com/en-us/library/ms178843.aspx

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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, 2013, 2016, 2019, and Word in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing All Open Documents

Have a bunch of documents you need to print? If all the documents are open, you can use a handy little macro to print ...

Discover More

Setting the AutoRecover Directory

Excel, by default, periodically writes information to AutoRecover files that can help protect your data in case Excel is ...

Discover More

Checking for Valid Hyperlinks

If you have a document containing a lot of hyperlinks, it would be cool if there was a way to check all those hyperlinks ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Automating a Manual Process with a Macro

The entire purpose of macros is to allow you to automate repetitive or tedious tasks with relative ease. How easy the ...

Discover More

Jumping to the Top of a Page

Do you want to easily jump to the top of a page in your document? You can use the Go To command to make the shift, or you ...

Discover More

Counting Words the Old Fashioned Way

One way to specify word count is to count characters and divide by five. If you still need this old-fashioned way of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 1?

2014-04-23 19:03:19

CMatz

Question: How to handle embedded spaces in the passed filename
Answer: Enclose it with double quotes and then strip them off in the macro
Question: How to handle passing more than one variable
Answer: Same technique, enclose each in double quotes, write them as multiple lines in the temporary text file.
An example for passing two variables is as follows:

---------------------------------SNIP------------------------------------------

@echo off
:Batch File SaveName2.bat
:Example to pass two parameters on command line
:CA Matz - Author
:Call from a command line as: SaveName2 "var1" "var2"
:Where var1 and var2 are variables to be passed to macro and can have embedded spaces
:Writes the variables passed to a temp file "---TMP---.txt" in the Users TEMP directory

ECHO %1 > %temp%---TMP---.txt
ECHO %2 >> %temp%---TMP---.txt


:Start Word with Macro ReadTemp
"C:Program Files (x86)Microsoft OfficeOffice14winword.exe" /MReadTemp2

---------------------------------SNIP------------------------------------------


Sub ReadTemp2()
'CA Matz - Author
Dim hNum As Integer
Dim strFile As String
Dim strLine1 As String
Dim strLine2 As String
Dim DQ As String
DQ = Chr(34)

hNum = FreeFile()

strFile = Environ("temp") & "---TMP---.txt"
Open strFile For Input As #hNum

Line Input #hNum, strLine1
' Strip the Double Quotes is they were passed for the pathfilename
strLine1 = Replace(strLine1, DQ, "")
MsgBox strLine1
Line Input #hNum, strLine2
' Strip the Double Quotes is they were passed for the pathfilename
strLine2 = Replace(strLine2, DQ, "")
MsgBox strLine2

' Your processing using variables strLine1 and strLine2 starts here instead of the message boxes

End Sub


2014-04-22 05:24:19

Emperorbilius

I use a macro to read information from a text file to populate fields in my Word document (version of document, owner, date of issue etc.). The text file is dynamically created by another program which writes the information to my text file.
I use the " GetPrivateProfileString" command to read the information into my Word document and the macro formats it.


2014-04-19 22:00:19

CMatz

Also sent the following as an email due to the text wrapping mess

Write the filename to a temp file with a batch file and then read the temp file within the Word macro to pass the variable

Batch file to save the filename to a temp file

---------------------------------SNIP------------------------------------------
@echo off
:Batch File SaveName.bat
:CA Matz - Author
:Call from a command line as: SaveName argument
:Where argument is the "pathfilename" to be passed to macro
:Writes the argument passed to a temp file "---TMP---.txt" in the Users TEMP directory

ECHO %1 > %temp%---TMP---.txt

: Note! for the drag and drop graphical batch processor FileViking use the following line instead
: ECHO %FV_FILE% > %temp%---TMP---.txt
: FileViking is found at FileViking.com

:Start Word with Macro ReadTemp
"C:Program Files (x86)Microsoft OfficeOffice14winword.exe" /MReadTemp

---------------------------------SNIP------------------------------------------

Word Subroutine to read the temp file into a variable called strLine

---------------------------------SNIP------------------------------------------

Sub ReadTemp()
'CA Matz - Author
Dim hNum As Integer
Dim strFile As String
Dim strLine As String

hNum = FreeFile()
strFile = Environ("temp") & "---TMP---.txt"
Open strFile For Input As #hNum

Line Input #hNum, strLine
MsgBox strLine
' Your processing starts here instead of the message box

End Sub

---------------------------------SNIP------------------------------------------


This Site

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.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.