Creating Text At Bookmark

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
 
E

Ed

Hi Bob,

I think the problem is that bookmarks are not preserved in a document
produced by mail merge. They have to be unique within a document and since
that condition would not be guaranteed in a merge, Word strips them out. This
is by design.

In your particular case, you might be able to insert the strMyText into the
mail merge main document itself prior to the merge (and after the merge,
close the MMMD and discard changes).

Another approach that can sometimes be used is to use "pseudo bookmarks"
rather than real ones, and after the merge, find the pseuds and replace with
what you want. E.g. we've sometimes used bits of text like "<<JobCode>>" as
place markers for this purpose.

Also, if you google the Word NGs for something lime "mail merge bookmark
unique" you should find other suggestions.

Regards.

Ed
 
B

Bob

Hi Bob,

I think the problem is that bookmarks are not preserved in a document
produced by mail merge. They have to be unique within a document and since
that condition would not be guaranteed in a merge, Word strips them out. This
is by design.

In your particular case, you might be able to insert the strMyText into the
mail merge main document itself prior to the merge (and after the merge,
close the MMMD and discard changes).

Another approach that can sometimes be used is to use "pseudo bookmarks"
rather than real ones, and after the merge, find the pseuds and replace with
what you want. E.g. we've sometimes used bits of text like "<<JobCode>>" as
place markers for this purpose.

Also, if you google the Word NGs for something lime "mail merge bookmark
unique" you should find other suggestions.

Regards.

Ed














- Show quoted text -

Thank you Ed.

I stepped through the code and added a piece that reports on existing
book marks (if any) - and none were found. This follows with what you
mentioned you your reply. I suppose I can add another field to the
document and either add text to it if the situation requires it or
pass a null to the field. I will have to experiment to see how to
make this work.

Thanks again.
Bob
 
E

Ed

You're welcome :)

Regards.

Ed

Bob said:
Thank you Ed.

I stepped through the code and added a piece that reports on existing
book marks (if any) - and none were found. This follows with what you
mentioned you your reply. I suppose I can add another field to the
document and either add text to it if the situation requires it or
pass a null to the field. I will have to experiment to see how to
make this work.

Thanks again.
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top