chrismanster
Programmer
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
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