D
david.roebuck
I hope someone can help me with this,I am trying to create a mail merge
from within my access database. The source of the data is a table which
I want to create mail merges based on a field name called Relationship
Manager. I want to open the word doc template and create the mail merge
then save it to a file then create the next mail merge and save that
etc.
The problem I am having is linking to the data source as it tries to
open the database again and also closing the word application
afterwards.
Public Sub Fire()
On Error GoTo Err_Fire
Dim db As Database
Dim rst As Recordset
Dim olkApp As Outlook.Application
Dim objmailitem As Outlook.MailItem
Dim objscript
Dim SafeItem
Dim utils
Dim strAttach As String
Dim strfilename As String
Dim objWord As Word.Document
Set objscript = CreateObject("Scripting.FileSystemObject")
If Not objscript.FolderExists("d:\" & "Fire\") Then
objscript.createfolder ("d:\" & "Fire\")
End If
If Dir("d:\Fire\*.doc") <> "" Then
Kill "d:\Fire\*.doc"
End If
Set objscript = Nothing
On Error Resume Next
Set db = CurrentDb
Set rst = db.OpenRecordset("tblrelmanager")
Do Until rst.EOF
Set objWord = GetObject("C:\Documents and
Settings\David\Desktop\fire risk assessment.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the WSS database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\David\Desktop\WSS.mdb", _
LinkToSource:=True, _
Connection:="table tblAccenture", _
SQLStatement:="SELECT * FROM [tblAccenture] WHERE [Relationship
Manager] = '" & rst![Relationship Manager] & "'"
' Execute the mail merge.
objWord.MailMerge.Execute
ActiveDocument.SaveAs FileName:="D:\fire\" & rst![Relationship
Manager] & ""
ActiveDocument.Close
rst.MoveNext
Loop
ActiveDocument.Close
objWord.Quit
rst.Close
Set rst = Nothing
db.Close
Set db = Nothing
Thanks in advance Dave
from within my access database. The source of the data is a table which
I want to create mail merges based on a field name called Relationship
Manager. I want to open the word doc template and create the mail merge
then save it to a file then create the next mail merge and save that
etc.
The problem I am having is linking to the data source as it tries to
open the database again and also closing the word application
afterwards.
Public Sub Fire()
On Error GoTo Err_Fire
Dim db As Database
Dim rst As Recordset
Dim olkApp As Outlook.Application
Dim objmailitem As Outlook.MailItem
Dim objscript
Dim SafeItem
Dim utils
Dim strAttach As String
Dim strfilename As String
Dim objWord As Word.Document
Set objscript = CreateObject("Scripting.FileSystemObject")
If Not objscript.FolderExists("d:\" & "Fire\") Then
objscript.createfolder ("d:\" & "Fire\")
End If
If Dir("d:\Fire\*.doc") <> "" Then
Kill "d:\Fire\*.doc"
End If
Set objscript = Nothing
On Error Resume Next
Set db = CurrentDb
Set rst = db.OpenRecordset("tblrelmanager")
Do Until rst.EOF
Set objWord = GetObject("C:\Documents and
Settings\David\Desktop\fire risk assessment.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the WSS database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\David\Desktop\WSS.mdb", _
LinkToSource:=True, _
Connection:="table tblAccenture", _
SQLStatement:="SELECT * FROM [tblAccenture] WHERE [Relationship
Manager] = '" & rst![Relationship Manager] & "'"
' Execute the mail merge.
objWord.MailMerge.Execute
ActiveDocument.SaveAs FileName:="D:\fire\" & rst![Relationship
Manager] & ""
ActiveDocument.Close
rst.MoveNext
Loop
ActiveDocument.Close
objWord.Quit
rst.Close
Set rst = Nothing
db.Close
Set db = Nothing
Thanks in advance Dave