S
Shannon Rotz
I have an Access XP procedure which automates Word XP, which is used to
create various Word mail merge documents in a database. It works for all of
the documents except one.
In the one that doesn't work, I keep getting Error 5922 "Cannot open the
data source". I've tried manually connecting using "Confirm conversion at
open" at picking "MS Access Databases", then picking ODBC, then the query,
but I get the same message.
Can anyone point me in the right direction to fix this document so that it
will use my code? I've posted it below. Note: the sqlstmt argument is:
"Select * from [{querydef created at run-time}]" (and yes, it is being
created).
Public Function WordMergeDSN(strDocName As String, sqlStmt As String,
AutoMerge As Boolean) As Boolean
On Error GoTo err_WordMergeDSN
Dim wrd As New Word.Application
Dim wrdDoc As Word.Document
Dim strConnection As String
Dim lngMembNo As Long
Dim blTried As Boolean
' OpenDataSource Method Example
wrd.Application.ChangeFileOpenDirectory CurrentDBDir
If strDocName = "New Document" Then
Set wrdDoc = wrd.Documents.Add
Else
Set wrdDoc = wrd.Documents.Add(strDocName)
End If
'Stop
wrd.ActiveWindow.View.ShowHiddenText = True
wrd.Visible = True
strConnection = "DSN=MS Access Database;DBQ=" & CurrentDb.NAME & ";"
With wrdDoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource NAME:=CurrentDB.Name, _
LinkToSource:=True, Connection:=strConnection, SQLStatement:=sqlStmt
If AutoMerge = True Then
.Execute True
End If
End With
If AutoMerge = True Then
wrd.Documents("Document1").Close wdDoNotSaveChanges
End If
WordMergeDSN = True
wrd.Activate
exit_WordMergeDSN:
If Not (wrdDoc Is Nothing) Then
Set wrdDoc = Nothing
End If
If Not (wrd Is Nothing) Then
Set wrd = Nothing
End If
Exit Function
err_WordMergeDSN:
Select Case Err.Number
Case Is = 5151
MsgBox ("Cannot find the template '" & strDocName & "'. Please
re-add it to the '" & CurrentDBDir & "' folder and try again.")
wrd.Quit
WordMergeDSN = False
Resume exit_WordMergeDSN
Case Is = 4198
Resume Next
Case 5922
If blTried = False Then
strConnection = "DSN=MS Access Database;DBQ=" &
CurrentDb.NAME & ";"
blTried = True
Resume
Else
MsgBox "Error #" & Err.Number & " - " & Err.Description
Resume exit_WordMergeDSN
End If
Case Else
MsgBox Err.Number & " - " & Err.Description
wrd.Quit
WordMergeDSN = False
Resume exit_WordMergeDSN
End Select
End Function
create various Word mail merge documents in a database. It works for all of
the documents except one.
In the one that doesn't work, I keep getting Error 5922 "Cannot open the
data source". I've tried manually connecting using "Confirm conversion at
open" at picking "MS Access Databases", then picking ODBC, then the query,
but I get the same message.
Can anyone point me in the right direction to fix this document so that it
will use my code? I've posted it below. Note: the sqlstmt argument is:
"Select * from [{querydef created at run-time}]" (and yes, it is being
created).
Public Function WordMergeDSN(strDocName As String, sqlStmt As String,
AutoMerge As Boolean) As Boolean
On Error GoTo err_WordMergeDSN
Dim wrd As New Word.Application
Dim wrdDoc As Word.Document
Dim strConnection As String
Dim lngMembNo As Long
Dim blTried As Boolean
' OpenDataSource Method Example
wrd.Application.ChangeFileOpenDirectory CurrentDBDir
If strDocName = "New Document" Then
Set wrdDoc = wrd.Documents.Add
Else
Set wrdDoc = wrd.Documents.Add(strDocName)
End If
'Stop
wrd.ActiveWindow.View.ShowHiddenText = True
wrd.Visible = True
strConnection = "DSN=MS Access Database;DBQ=" & CurrentDb.NAME & ";"
With wrdDoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource NAME:=CurrentDB.Name, _
LinkToSource:=True, Connection:=strConnection, SQLStatement:=sqlStmt
If AutoMerge = True Then
.Execute True
End If
End With
If AutoMerge = True Then
wrd.Documents("Document1").Close wdDoNotSaveChanges
End If
WordMergeDSN = True
wrd.Activate
exit_WordMergeDSN:
If Not (wrdDoc Is Nothing) Then
Set wrdDoc = Nothing
End If
If Not (wrd Is Nothing) Then
Set wrd = Nothing
End If
Exit Function
err_WordMergeDSN:
Select Case Err.Number
Case Is = 5151
MsgBox ("Cannot find the template '" & strDocName & "'. Please
re-add it to the '" & CurrentDBDir & "' folder and try again.")
wrd.Quit
WordMergeDSN = False
Resume exit_WordMergeDSN
Case Is = 4198
Resume Next
Case 5922
If blTried = False Then
strConnection = "DSN=MS Access Database;DBQ=" &
CurrentDb.NAME & ";"
blTried = True
Resume
Else
MsgBox "Error #" & Err.Number & " - " & Err.Description
Resume exit_WordMergeDSN
End If
Case Else
MsgBox Err.Number & " - " & Err.Description
wrd.Quit
WordMergeDSN = False
Resume exit_WordMergeDSN
End Select
End Function