Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DSN Connection for MailMerge

Status
Not open for further replies.

chrismanster

Programmer
Jan 23, 2008
5
0
0
US
I am trying to connect to a data source via a DSN connection, when the OpenDataSource command executes, the script crashes. Here is the code:

Sub GenerateDocument(id)
Dim oApp, oDoc, oSel

Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add

With oDoc.MailMerge
.MainDocumentType = wdFormLetters
On Error Resume Next
.OpenDataSource "", wdOpenFormatAuto, false, true, false, false, "", "", true, "", "", "DSN=<dsn_here>;UID=<uid>;PWD=<password>", "SELECT * FROM EMP WHERE EMPNO = " & id, "", false, wdMergeSubTypeWord

If Err.Number <> 0 Then
msgbox "Error Number: " & Err.Number & vbCrLf & Err.description
End If

On Error GoTo 0
With .Fields
Set oSel = oApp.Selection
oSel.TypeText "Dear "
.Add oSel.Range, "JOB"
oSel.TypeText " "
.Add oSel.Range, "ENAME"
oSel.TypeText ","
End With
End With
End Sub
 
the script crashes"

What is the error. What line generates the error specifically?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Error Number: 4198
Error Description: Command Failed

It is this line:
.OpenDataSource "", wdOpenFormatAuto, false, true, false, false, "", "", true, "", "", "DSN=&lt;dsn_here&gt;;UID=&lt;uid&gt;;PWD=&lt;password&gt;", "SELECT * FROM EMP WHERE EMPNO = " & id, "", false, wdMergeSubTypeWord
 
Tip: use the Option Explicit instruction.
I guess that you haven't defined wdFormLetters, wdOpenFormatAuto, ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I agree w/ PHV; I think encountered this problem w/ instantiating XL and using internal XL references. You'll need to access the object browser in Word or echo those out and find the real values use them or set them as constants in your VBS if you want to use.

[yinyang] Tranpkp [pc2]
 
Thanks, I changed those constants to the numeric values and it worked perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top