Passing filename for mail merge into a macro

E

ewout.boter

Some time ago, people in this newgroup have helped me out regarding a
problem with a Word macro. Now, I have another question...

The code of the macro is as follows:

Sub InsurancePolicy()
'
' InsurancePolicy Macro
' Macro recorded 9/9/2008 by Boter
'
Dim maindoc As Document
Set maindoc = Documents.Open(FileName:="\\ainl6f001\inf$\data
\Development\InsurancePolicy\InsurancePolicyTemplate.doc",
ReadOnly:=True, Visible:=False)
With maindoc.MailMerge
.Destination = wdSendToNewDocument
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With

maindoc.Close wdDoNotSaveChanges

End Sub

The location and the name of the template document are fixed. This is
no problem. However, the name of the file containing the data to be
merged will not be fixed. Therefore, I would like to pass this name
into the macro in some way.

Maybe I should point that Word will be started automatically from a
program on an iSeries (f.k.a. AS/400) server, using the command
STRPCCMD PCCMD('"C:\Program Files\Microsoft Office\Office
\WINWORD.exe" /mInsurancePolicy').

Is there a way to specify a parameter in the previous command? Or is
there some other way to accomplish what I want?

Thanks in advance.

Ewout
 
S

Steve Yandl

Ewout,

The only way I've found to feed arguments to a Word macro from the command
line is to use a vbScript file as an intermediary. The vbScript file is a
simple text file that is given the file extension of vbs rather than txt.
The actual executable that runs such a file is either WScript.exe or
CScript.exe. Using the vbs approach allows you to take advantage of the
Arguments property of the WScript object to grab arguments from the command
line and then you have the script deliver the arguments to the Word
subroutine.

For a test, I created a new document "C:\Test\PetList.doc". In that
document, I created a macro named "AddPets" that takes two string arguments.

Next, I created the vbScript file using Notepad and saved it as
"C:\Test\LaunchWordSub.vbs" The content of this vbs is below between the
dotted lines.

' -------------------------------------------------

arg1 = WScript.Arguments(0)
arg2 = WScript.Arguments(1)

Set objWd = CreateObject("Word.Application")
objWd.Visible = True

objWd.Documents.Add("C:\Test\PetList.doc")

objWd.Run "AddPets", CStr(arg1), CStr(arg2)

' -------------------------------------------------

The arguments I want to send to my AddPets subroutine within PetList.doc are
"cat" and "dog" To accomplish this, I use the command line:
WScript.exe "C:\Test\LaunchWordSub.vbs" "cat" "dog"

You may find that you need to include the full path to the executable file
"WScript.exe". On my PC, the command line tests fine as is from the 'Start
Run' line.

Not the most direct approach but it works. It might be simpler to write the
script to include the code from the macro but since you already have the
macro written and tested you might as well do it this way.


Steve Yandl
 
D

Doug Robbins - Word MVP

From where will the name of the data file come?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top