Mailmerge and Bookmarks

S

Sue Rizzo

I have a mailmerge main doc that contains bookmarks. When the doc is merge
into the new doc, the bookmarks are gone. Can anyone tell me how to prevent
the bookmarks from disappearing?
 
D

Doug Robbins

The name of each bookmark in a document must be unique, therefore, they
cannot survive the merge process. Maybe you can make use of the following
macro to "re-create" the bookmarks

' Throwaway Macro created by Doug Robbins to "preserve" bookmarks during a
MailMerge

'

Dim abm As Bookmark, bmrange As Range, i As Long, Result As Document, j As
Long, k As Long, linkrange As Range, linktarget As String

Dim Source As Document

Set Source = ActiveDocument

i = 1

For j = 1 To Source.MailMerge.DataSource.RecordCount

For Each abm In ActiveDocument.Range.Bookmarks

System.PrivateProfileString("c:\bookmarks.txt", "bookmarkNames",
"bookmark" & i) = abm.Name & Format(j)

i = i + 1

Next

Next j

For Each abm In ActiveDocument.Range.Bookmarks

abm.Range.InsertBefore "#"

abm.Range.InsertAfter "#"

Next

With ActiveDocument.MailMerge

.Destination = wdSendToNewDocument

.Execute

End With

Set Result = ActiveDocument

k = 1

Selection.HomeKey wdStory

Selection.Find.ClearFormatting

With Selection.Find

Do While .Execute(FindText:="#*#", MatchWildcards:=True,
Wrap:=wdFindContinue, Forward:=True) = True

Set bmrange = Selection.Range

bmrange.Characters(bmrange.Characters.Count).Delete

bmrange.Characters(1).Delete

Result.Bookmarks.Add System.PrivateProfileString("c:\bookmarks.txt",
"bookmarkNames", "bookmark" & k), bmrange

k = k + 1

Loop

End With


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
S

Sue Rizzo

Thanks Doug!
It's unfortunate that bookmarks can't survive the merge process, they are
unique within each document (the main and the new) so I'm not sure I
understand the problem with their survival to the new doc. I do appreciate
the macro though I had trouble with the syntax of 2 lines:
For j = 1 To Source.MailMerge.DataSource.RecordCount

..RecordCount is not available so I used .ActiveRecord

and
Result.Bookmarks.Add System.PrivateProfileString("c:\bookmarks.txt",
"bookmarkNames", "bookmark" & k), bmrange

I'm not sure what this is doing, I am a beginner with vb code so I'm not
sure how to correct.
Thanks for your help!!
Sue
 
D

Doug Robbins

You have to have the mailmerge main document with a datasource attached to
it when you run the code. That should deal with the first issue.

The second is caused by what is a single line of code wrapping in the mail
program. Remove the carriage return at the end of the first line so that it
is restored to a single line in the Visual Basic Editor.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

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