Lost Bookmarks when mail merging

G

Graham

Hi there

I have the following code that creates a mail merge final document and names
it then allows the user to edit the remaining document. This all works fine
but I have added dookmarks to the template to use for selection of text but
these bookmarks are missing. Where have I gone wrong with my code?

Sub LC1()
'
' LETTER Macro
' Macro created 07/11/2006 by Graham Dingley
'
Application.WindowState = wdWindowStateMinimize
Dim MMMDoc As Document
' This part creates a new document based on the open template
Set MMMDoc = Documents.Open("c:\customers\nelsons\cravefamily.dot")
'***************************************
ActiveDocument.MailMerge.OpenDataSource Name:="C:\WPDOCS\LC1.DOC", _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="",
SQLStatement1 _
:=""
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False

With MMMDoc.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With

MMMDoc.Close SaveChanges:=wdDoNotSaveChanges
Set MMMDoc = Nothing

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="CasePath"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
' This part goes to the start of the document and sets up a Bookmark
called CasePath using the field CASEPATH from Lawmaster
CasePath = ActiveDocument.Bookmarks("CasePath").Range.Text
ChangeFileOpenDirectory CasePath
ActiveDocument.SaveAs FileName:="Family Crave - " & Format(Now(),
"dd-mm-yyyy hh-mm-ss") & ".doc", FileFormat:=wdFormatDocument,
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False

' After saving the document the bookmark is removed then document is
resaved, the cursor is sent to the end of the document and Word is maximised
to allow the user to carry on typing

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
CommandBars("Mail Merge").Visible = False

ActiveDocument.Save
Selection.EndKey Unit:=wdStory
Application.WindowState = wdWindowStateMaximize

Selection.EndKey Unit:=wdStory
Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
CommandBars("Mail Merge").Visible = False
ActiveDocument.Save
Application.WindowState = wdWindowStateMaximize
Selection.HomeKey Unit:=wdStory

End Sub

Thanks in advance for any help.
 
C

Cindy M.

Hi Graham,
I have the following code that creates a mail merge final document and names
it then allows the user to edit the remaining document. This all works fine
but I have added dookmarks to the template to use for selection of text but
these bookmarks are missing. Where have I gone wrong with my code?
You haven't done anything wrong - Word is working as designed. The problem is
that a bookmark name must be unique. Mail merge merges multiple records, making
copies of the document as it goes. This means it would have to duplicate
bookmarks, which is not possible. So the bookmarks are removed.

A typical approach is to put a unique set of characters at "target points" then
use Find on the result document to locate these places in the result document.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
G

Graham

Hi Cindy

Thanks for that. Good to know my coding wasn't causing that.

I was going to use a Userform to select various options to delete certain
clause in a document using bookmarks. These unique characters are I assume
just random characters that will also print.

Regards
 
C

Cindy M.

Hi Graham,
I was going to use a Userform to select various options to delete certain
clause in a document using bookmarks. These unique characters are I assume
just random characters that will also print.
Yes, they would print. But if you were to use Find they would be deleted as
part of that code.

However...

Could you possibly use an IF field to display/hide the text? The user form
could, for example, write the information to document Variable objects. The
If field could check the result of corresponding DocVariable fields and
selectively display/hide the clauses. The field code would look roughly like:

{ IF "{DocVariable Test1}"="Show" "Clause text here" "" }

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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