ordendelfai
Technical User
Hi Everyone!
I am trying to solve a small problem that is occuring with mail merge testing that I am doing with the below code. When I mailmerge, Word opens, performs the merge, prints, and closes both the template, and the merged document w/out asking the user if he wants to save. This is exactly how we want it, however, If the user already has a Word instance open (and is working on something), then Word obviously tries to close that document too and a "Do you want to save" box comes up.
I am not overly familiar with VBA, but I am trying to find a line of code that will always open this mail merge in a NEW Word instance, not the existing one already open.
A second problem is I am using a variant of the code below for a few special mail merges that do not automatically print/close word. So if the user closes the merged document and keeps word open (no documents), and tries to merge something else later, an error "Couldn't lock table "tblMailMerge1Employee". currently in use by user 'Admin' on machine 'IS-0047606'. I assume I just need a line of code that closes the connection to the DB (because if the user closes Word altogether, then tries to merge something else, it works fine).
If you can help with either or both these questions I would really appreciate it!! =D
Joel
-----------------------------Code---------------------------
Public Function MergeIt1EmployeePrint()
Dim objWord As Word.Document
Set objWord = GetObject("S:\Delphi\Templates\ExternalCorrespondence\1EmployeeTermGroup.doc", "Word.Document"
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Access database.
objWord.MailMerge.OpenDataSource _
Name:="C:\MyDB\Database.mdb", _
LinkToSource:=False, _
Connection:="TABLE tblMailMerge1Employee"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
'The following line must follow the Execute statement because the
'PrintBackground property is available only when a document window is
'active. Without this line of code, the function will end before Word
'can print the merged document.
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
objWord.Application.ActiveDocument.Saved = True
objWord.Saved = True
If WordWasNotRunning = False Then
objWord.Application.Quit
End If
End Function
-----------------------------Code---------------------------
I am trying to solve a small problem that is occuring with mail merge testing that I am doing with the below code. When I mailmerge, Word opens, performs the merge, prints, and closes both the template, and the merged document w/out asking the user if he wants to save. This is exactly how we want it, however, If the user already has a Word instance open (and is working on something), then Word obviously tries to close that document too and a "Do you want to save" box comes up.
I am not overly familiar with VBA, but I am trying to find a line of code that will always open this mail merge in a NEW Word instance, not the existing one already open.
A second problem is I am using a variant of the code below for a few special mail merges that do not automatically print/close word. So if the user closes the merged document and keeps word open (no documents), and tries to merge something else later, an error "Couldn't lock table "tblMailMerge1Employee". currently in use by user 'Admin' on machine 'IS-0047606'. I assume I just need a line of code that closes the connection to the DB (because if the user closes Word altogether, then tries to merge something else, it works fine).
If you can help with either or both these questions I would really appreciate it!! =D
Joel
-----------------------------Code---------------------------
Public Function MergeIt1EmployeePrint()
Dim objWord As Word.Document
Set objWord = GetObject("S:\Delphi\Templates\ExternalCorrespondence\1EmployeeTermGroup.doc", "Word.Document"
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Access database.
objWord.MailMerge.OpenDataSource _
Name:="C:\MyDB\Database.mdb", _
LinkToSource:=False, _
Connection:="TABLE tblMailMerge1Employee"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
'The following line must follow the Execute statement because the
'PrintBackground property is available only when a document window is
'active. Without this line of code, the function will end before Word
'can print the merged document.
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
objWord.Application.ActiveDocument.Saved = True
objWord.Saved = True
If WordWasNotRunning = False Then
objWord.Application.Quit
End If
End Function
-----------------------------Code---------------------------