B
Bob
I an trying to use Access 2002 to enter text at a bookmark in a Word
template. Below is the code, which is not working. Error 'Requested
member of collection does not exists." Error # 5941. The bookmark's
name is 'Insert1', not meaningful, but good for testing.
Code.
Public Sub MailMerge()
'-------------------------------------------------------------
' Procedure : MailMerge
' Purpose : Merge table's data with Word one of three Word templates
' located in the 'S' drive.
' Author : Bob
' Phone: 916-xxx-xxxx
' E-Mail: (e-mail address removed)
' DateTime : 7/27/2007 08:04
' Notes : Code copied in part from Access Cookbook, O'Reilly, chapter
12.4,
' pages 563-567.
' Tables: tmpLetters
' Queries: qryMergeToWord
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
'
'=============================================================
Dim strPath As String
Dim strDataSource As String
Dim strTemplate As String
Dim Doc As Word.Document
Dim wrdApp As Word.Application
Dim curDoc As Object
Dim strMyText As String
On Error GoTo HandleErrors
strMyText = "If you wish to contact me to further discuss the
outcome, " _
& " please call (916)350-7464, and have the following information
available:" _
& vbCrLf & vbCrLf & "Patient Name and Subscriber ID#" _
& vbCrLf & "Clinical circumstances you feel will affect the
determination" _
& vbCrLf & "Applicable dates" & vbCrLf & vbCrLf
Select Case Me.cboGrievanceInf
Case Is = "CalPers"
strTemplate = "CalPersDenialWordMerge.dot"
Case Is = "DOI"
strTemplate = "DoiDenialWordMerge.dot"
Case Is = "DMHC"
strTemplate = "DmhcDenialWordMerge.dot"
End Select
' Delete the rtf file, if it already exists.
strPath = FixPath(CurrentProject.Path)
strDataSource = strPath & conQuery & ".doc"
Kill strDataSource
' Export the data to rtf format
DoCmd.OutputTo acOutputQuery, conQuery, _
acFormatRTF, strDataSource, False
' Start Word using mailmerge template
Set wrdApp = New Word.Application
Set Doc = wrdApp.Documents.Add(strPath & strTemplate)
' Do the mail merge to a new document.
With Doc.MailMerge
.OpenDataSource Name:=strDataSource
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
If .State = wdMainAndDataSource Then .Execute
End With
' Display the mail merge document
wrdApp.Visible = True
'************
wrdApp.ActiveDocument.Bookmarks("Insert1").Range.Text = strMyText
'**********
ExitHere:
Set curDoc = Nothing
Set Doc = Nothing
Set wrdApp = Nothing
Exit Sub
HandleErrors:
Select Case Err.Number
Case 53 ' File not found
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Select
Resume
End Sub
Thanks,
Bob
template. Below is the code, which is not working. Error 'Requested
member of collection does not exists." Error # 5941. The bookmark's
name is 'Insert1', not meaningful, but good for testing.
Code.
Public Sub MailMerge()
'-------------------------------------------------------------
' Procedure : MailMerge
' Purpose : Merge table's data with Word one of three Word templates
' located in the 'S' drive.
' Author : Bob
' Phone: 916-xxx-xxxx
' E-Mail: (e-mail address removed)
' DateTime : 7/27/2007 08:04
' Notes : Code copied in part from Access Cookbook, O'Reilly, chapter
12.4,
' pages 563-567.
' Tables: tmpLetters
' Queries: qryMergeToWord
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
'
'=============================================================
Dim strPath As String
Dim strDataSource As String
Dim strTemplate As String
Dim Doc As Word.Document
Dim wrdApp As Word.Application
Dim curDoc As Object
Dim strMyText As String
On Error GoTo HandleErrors
strMyText = "If you wish to contact me to further discuss the
outcome, " _
& " please call (916)350-7464, and have the following information
available:" _
& vbCrLf & vbCrLf & "Patient Name and Subscriber ID#" _
& vbCrLf & "Clinical circumstances you feel will affect the
determination" _
& vbCrLf & "Applicable dates" & vbCrLf & vbCrLf
Select Case Me.cboGrievanceInf
Case Is = "CalPers"
strTemplate = "CalPersDenialWordMerge.dot"
Case Is = "DOI"
strTemplate = "DoiDenialWordMerge.dot"
Case Is = "DMHC"
strTemplate = "DmhcDenialWordMerge.dot"
End Select
' Delete the rtf file, if it already exists.
strPath = FixPath(CurrentProject.Path)
strDataSource = strPath & conQuery & ".doc"
Kill strDataSource
' Export the data to rtf format
DoCmd.OutputTo acOutputQuery, conQuery, _
acFormatRTF, strDataSource, False
' Start Word using mailmerge template
Set wrdApp = New Word.Application
Set Doc = wrdApp.Documents.Add(strPath & strTemplate)
' Do the mail merge to a new document.
With Doc.MailMerge
.OpenDataSource Name:=strDataSource
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
If .State = wdMainAndDataSource Then .Execute
End With
' Display the mail merge document
wrdApp.Visible = True
'************
wrdApp.ActiveDocument.Bookmarks("Insert1").Range.Text = strMyText
'**********
ExitHere:
Set curDoc = Nothing
Set Doc = Nothing
Set wrdApp = Nothing
Exit Sub
HandleErrors:
Select Case Err.Number
Case 53 ' File not found
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Select
Resume
End Sub
Thanks,
Bob