PDA

View Full Version : Access mailmerge



glover
12-01-2008, 04:23 AM
This may look daunting because of the amount of code but my problem is quite simple and is probably just a wrong word somewhere.
The idea behind the code is from Access button click it launches word or uses the current one thats open and then performs the mailmerge and then closes.
Problem im having is that it works fine if word isnt open but when it is, it fails.
Ive adapted the code to get around this but then I lose my varible appWord since its not used to open the doc and then I dont know what to SET it to.

Dim docWord As Word.Document
Dim appword As Word.Application
Dim Path As String
Dim FD As String

' Open the document
Set docWord = GetObject("C:\Letters\" & orgFile, "Word.Document")

If Err.Number <> 0 Then

' Create the Word application.
Set appword = CreateObject("Word.Application")

' Make Word visible.
appword.Visible = True

' Open the document
Set docWord = GetObject("C:\Letters\" & orgFile, "Word.Document")

End If

'Merge the document
With docWord.MailMerge
.OpenDataSource _
Name:="C:\Letters\DB.mdb", _
LinkToSource:=True, _
Connection:="VIEWS " & conxName, _
SQLStatement:="SELECT * FROM" & qryName

' Merge to new document
.Destination = wdSendToNewDocument
.DataSource.FirstRecord = wdDefaultFirstRecord
.DataSource.LastRecord = wdDefaultLastRecord
.Execute
End With

' Set info for "new merged" to be saved
Path = "C:\Letters\"
FD = Format(Date, "yymmdd")

'Create the folder to save if doesnt exist
If Len(Dir(Path & FD & "\", vbDirectory)) = 0 Then
MkDir Path & FD & "\"
End If

Word.Application.Quit

'Release objects
Set docWord = Nothing
Set appword = Nothing


Any help would be greatly appreciated
Thanks