I do know what query I need, although obviously I will
be passing it a variable. Problem is, how would I pass trhat variable to
Word to tell it which record to pull back.
The problem is that the standard Command method in VBA in Office cannot get
the whole command line passed to Word. You may be able to get the whole
command line using the following code based on a posting by a Word MVP
called Will Rickards, I think:
---------------------------------------------------------------------------------
Public Declare Function GetCommandLine Lib "kernel32" Alias
"GetCommandLineA" () As Long
Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal
lpString1 As String, ByVal lpString2 As Long) As Long
Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal
lpString As Long) As Long
Public Function LPTSTRtoString(ByVal lngPtr As Long) As String
Dim strReturn As String
Dim lngStrLen As Long
'get the length of the string (not including the terminating null character)
lngStrLen = lstrlen(lngPtr)
'initialize our string so it has enough characters including the null
character
strReturn = String$(lngStrLen + 1, 0)
'copy the string we have a pointer to into our new string
lstrcpy strReturn, lngPtr
'now strip off the null character at the end
strReturn = Left$(strReturn, lngStrLen)
'return the string
LPTSTRtoString = strReturn
End Function
Public Sub AutoOpen()
Dim strCommandLine As String
Dim strDirectory As String
'get the commandline
strCommandLine = LPTSTRtoString(GetCommandLine())
' but then you will have to process the command line to get you parameter.
' and do what you need with it
End Sub
---------------------------------------------------------------------------------
Otherwise, if you are /automating/ from outside Word, for example using
VBScript, then what you do is
a. create a new Word object
b. open the MailMerge Main document (which will then try to connect to the
existing data source)
c. set the value of QueryString
d. execute the merge (and unfortunately, it may not be easy to see when it
has finished if you are merging to printer or email)
e. close everything
f. quit word
It's a bit difficult to show you how without knowing more about your
starting point, but if you search this group for messages containing the
word "CreateObject", you'll find plenty of examples of how automation is
usually done (but probably not many that script from /outside/ Word)
What about if I use ASK in mailmerge, and take that input and pass it to
the
SQL query?
At this point it's too late, and there's no way to get the ASK to modify the
query. However, if you are happy to do something along those lines you could
have a Word VBA AutoOpen macro that
a. used the inputbox function to display the prompt and gather a record
number (interaction required)
b. set .Querystring
c. did the merge etc.