"Abby" <why...@hotmail.com> wrote in message
news:101c201c217dd$e75b3240$b1e62ecf@tkmsftngxa04...
"Abby" <why...@hotmail.com> wrote in message
news:101c201c217dd$e75b3240$b1e62ecf@tkmsftngxa04...
>
I made a function that mailmerge a document with a query. if the document is
a mailmerge document.
Sorry, the msgbox languange en functionname is dutch
Public Function Samenvoegen(WordDocName As String, QueryName As String,
Optional Printen As Boolean)
On Error GoTo Err_Samenvoegen
Dim oApp As Object
Dim dbs As Database
Dim strPad As String
Dim worddoc As Object
Dim blnGevonden As Boolean
Dim QDF As QueryDef
Dim rs As Recordset
DoCmd.Hourglass True
Set dbs = CurrentDb()
strPad = dbs.Name
blnGevonden = False
For Each QDF In dbs.QueryDefs
If QDF.Name = QueryName Then
blnGevonden = True
End If
Next QDF
If blnGevonden = False Then
MsgBox ("De query is niet gevonden"), vbExclamation
GoTo eind
End If
Set rs = dbs.OpenRecordset(QueryName, dbOpenDynaset)
If Not rs.RecordCount > 0 Then
MsgBox ("Er zijn geen gegeven voor deze brief")
GoTo eind
End If
Set oApp = CreateObject("Word.Application")
If Dir(WordDocName) & " " <> " " Then
oApp.Documents.Open (WordDocName)
VerderlegeGegevensBrief:
Set worddoc = oApp.Documents.Item(oApp.ActiveDocument.Name)
Else
MsgBox ("Het bestand is niet gevonden"), vbExclamation
oApp.Quit
GoTo eind
End If
With worddoc.MailMerge
.OpenDataSource strPad, linktosource:=True, addtorecentfiles:=False,
Connection:="QUERY " & QueryName
End With
If worddoc.MailMerge.State = 2 Then 'wdMainAndDataSource
If Not IsMissing(Printen) Then
worddoc.MailMerge.Destination = Abs(CLng(Printen))
Else
worddoc.MailMerge.Destination = 0 'wdSendToNewDocument
End If
worddoc.MailMerge.Execute
End If
worddoc.Close SaveChanges:=0 'wdDoNotSaveChanges
DoCmd.Hourglass False
If Printen = True Then
oApp.Quit
Else
oApp.Visible = True
End If
eind:
DoCmd.Hourglass False
Set worddoc = Nothing
Set oApp = Nothing
Exit_Samenvoegen:
Exit Function
Err_Samenvoegen:
If Err.Number = 5631 Then Resume VerderlegeGegevensBrief
MsgBox Err.Description
Resume Exit_Samenvoegen
End Function
You can then run the form just like a report.
I've used this technique whenever I'm doing a mailmerge project.
( This is also neat especially when the users/s do no have Msword
Loaded. )
"Abby" <why...@hotmail.com> wrote in message
news:101c201c217dd$e75b3240$b1e62ecf@tkmsftngxa04...
>