R
Renee Moffett
I have code behind a form that works fine to produce a mail merge letter in
word. However, we are transitioning machines in my office. Previously, the
code that opened the mail merge source could be directed to the same path
for all machines. Now, some machines will have the front end on the D drive
(the old method) and newer machines will have it on C (their only drive).
Can someone offer a suggestion to modify the following code so that it will
work on either type of machine. Thanks.
Private Sub Print_Click()
Dim strTemplateName As String
Dim objWordApp As Word.Application
Dim wddoc As Word.Document
strTemplateName = DLookup("[FileName]", "tblFormLetterAddresses",
"[TemplateID]=" & Me!frameLetterChoices)
Set objWordApp = CreateObject("Word.Application")
With objWordApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
'Create New Document from Template
Set wddoc = objWordApp.Documents.Add(Template:=strTemplateName,
NewTemplate:=False)
'Select ODBC Data Source
wddoc.MailMerge.OpenDataSource Name:= _
"D:\My Databases\Atlas Database 1.0.mdb", ConfirmConversions:=False,
_
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto,
_
Connection:= _
"DSN=MS Access 97 Database;DBQ=D:\My Databases\Atlas Database
1.0.mdb;DriverId=281;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
, SQLStatement:="SELECT * FROM `tblFormLetter`", SQLStatement1:=""
'Mail Merge
With wddoc.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
wddoc.CLOSE SaveChanges:=wdDoNotSaveChanges
End With
Set wddoc = Nothing
Set objWordApp = Nothing
End Sub
word. However, we are transitioning machines in my office. Previously, the
code that opened the mail merge source could be directed to the same path
for all machines. Now, some machines will have the front end on the D drive
(the old method) and newer machines will have it on C (their only drive).
Can someone offer a suggestion to modify the following code so that it will
work on either type of machine. Thanks.
Private Sub Print_Click()
Dim strTemplateName As String
Dim objWordApp As Word.Application
Dim wddoc As Word.Document
strTemplateName = DLookup("[FileName]", "tblFormLetterAddresses",
"[TemplateID]=" & Me!frameLetterChoices)
Set objWordApp = CreateObject("Word.Application")
With objWordApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
'Create New Document from Template
Set wddoc = objWordApp.Documents.Add(Template:=strTemplateName,
NewTemplate:=False)
'Select ODBC Data Source
wddoc.MailMerge.OpenDataSource Name:= _
"D:\My Databases\Atlas Database 1.0.mdb", ConfirmConversions:=False,
_
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto,
_
Connection:= _
"DSN=MS Access 97 Database;DBQ=D:\My Databases\Atlas Database
1.0.mdb;DriverId=281;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
, SQLStatement:="SELECT * FROM `tblFormLetter`", SQLStatement1:=""
'Mail Merge
With wddoc.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
wddoc.CLOSE SaveChanges:=wdDoNotSaveChanges
End With
Set wddoc = Nothing
Set objWordApp = Nothing
End Sub