We are attempting to automate a mail merge process from within Access - upon clicking a button, VBA runs that specifies the current database (an accdb) as the data source and runs SQL, per the code below:
'Set up Word
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
'Make visible, open specified doc to merge
With objWord
.Visible = True
.Documents.Open strDocName
End With
'Set the data source path
Dim docPath As String
docPath = CurrentProject.Path & "\" & CurrentProject.Name
'Open the merge data source - simplified
objWord.activedocument.mailmerge.opendatasource _
Name:=docPath, _
SQLStatement:=strSQL
strDocName and strSQL are passed in and contain valid, functional contents.
It succeeds in opening word, making it visible and opening the template to merge into. At that point, however, it comes up asking to confirm data source, and shows a list of possible data sources. Upon clicking the 'show all' checkbox, I can scroll down to MS Access Database via ODBC (.mdb, .accdb) and select it from the list.
It then presents with a box titled 'ODBC Microsoft Access Driver Login Failed', saying 'Could Not Find File: "[path to database folder]/[name of containing folder].mdb"'
So, if my database were located in C:\Temp the error path would read 'C:\Temp.mdb'. Moving the access database to another folder causes the error path to update looking for an accordingly named mdb file based on the containing folder.
I don't know if it's relevant but it also appears to be opening the template in the mode for editing the template itself, rather than generating a new document from that template.
Am I missing something here or does anyone have any ideas?
Cheers
With the assistance of Remou below, we gave up linking directly to Access and used the code from Remou's suggested link to output to a temporary text file, then merge from there.
We modified the code to remove CurrentBackendPath() and changed the only reference to this function as follows:
Private Function GetStartDirectory() As String
'GetStartDirectory = CurrentBackendPath() & "mm\"
GetStartDirectory = CurrentProject.Path & "\mm\"
End Function
The only other modifications we used were to change the code to be specific to our purpose. Thanks once again to everyone for your replies. I would upvote Remou for their response if I had enough reputation to do so!