Question : Mail merge selected records into Word

Hi,

I currently have a form which can be filtered to show only certain records.
On that form, I have a "Mail merge" button which should mail merge the filtered records into an existing Word document.

Here's the code for the button:

Dim strSQL, sql As String
strSQL = Me.Form.Filter
sql = "SELECT * FROM Prospects WHERE " & strSQL

' create new word document
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Set WordApp = CreateObject("Word.Application")

WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("y:\database\mailshot1.doc")

WordApp.Application.DisplayAlerts = wdAlertsNone

With WordApp.ActiveDocument.MailMerge

    .MainDocumentType = wdFormLetters
    .OpenDataSource _
      Name:="Y:\database\Prospects Database.mdb ", _
      LinkToSource:=True, _
      Connection:="TABLE Prospects", _
      SQLStatement:=sql

    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    .Execute True
   
End With


If I step through the code, I can see that it is opening the document correctly, but when it gets to the OpenDataSource line it brings up a "Confirm data source" window in Word, with the following options:

OLE DB Database Files
MS Access Databases via DDE (*.mdb;*.mde)
MS Access Database via ODBC (*.mdb)
Xtreme Sample Database 2005 via ODBC (*.mdb)
Xtreme Sample Database 2003 via ODBC (*.mdb)

If I select the third option, It then asks for a table name.
Once this is complete, it successfully does the mail merge.

Is there any way to suppress these dialog messages by altering my code above?

Many thanks in advance.

Answer : Mail merge selected records into Word

The technique I use (for access 2000) is to dump query results into a csv (which I call .dat) file and use that.  This doesn't reuslt in any spurious dialogue boxes (or spurious instances of access which are always a possibility)

Dim wApp As Word.Application
Dim wSDoc As New Word.Document     'Source document
Dim wDDoc As New Word.Document     'Destination document
Dim fso As New FileSystemObject

etc...


'Create textfile and dat file
strTextFile = strBaseFolder & "NHPRMM.txt"
strDatFile = strBaseFolder & "NHPRMM.dat"

'Delete the files if they are aleady there
If fso.FileExists(strTextFile) Then
    fso.DeleteFile strTextFile
End If

'Create the merge file
DoCmd.TransferText acExportDelim, , "tblMM", strTextFile, True
fso.CopyFile strTextFile, strDatFile


Set wApp = New Word.Application
Set wSDoc = wApp.Documents.Open(strMMSource)

With wSDoc
    .MailMerge.MainDocumentType = wdFormLetters
    .MailMerge.OpenDataSource Name:=strDatFile, Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", LinkToSource:=True
    .MailMerge.Destination = wdSendToNewDocument
    .MailMerge.Execute
End With

'Grab the newly merged document
Set wDDoc = wApp.ActiveDocument

'Close down the original source file
wSDoc.Close

wDDoc.PrintOut
wDDoc.Close SaveChanges:=False
Set wApp = Nothing
Random Solutions  
 
programming4us programming4us