Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / August 2007

Tip: Looking for answers? Try searching our database.

Preserve Bookmark in Mailmerge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Slash - 21 Aug 2007 00:01 GMT
I am using a system that interfaces with MS/Word.  The system allows me to
create some mailmerge documents.

The first document is a source document that holds a single merge field on a
line as the only content.

{ MERGEFIELD Reference_Number }

When this resolves it creates a document C:\TEMP\\LAP\\INTDLIFE.DOC
containing the value of a database field called Reference_Number.

A second document (also a mailmerge), pulls in the first document using

{ INCLUDETEXT  "c:\\temp\\LAP\\INTDLIFE.DOC" }

Now the problem is, when it includes the document it also includes the
paragraph marker following the mergefield, and thus in the final resolved
document their is an unwanted line break inserted, resulting in a blank line.

I have read that this can be removed by defining a bookmark in the original
document containing the merge field, by defining the bookmark over the
mergefield but excluding the paragraph marker.  Then in the second document
expanding the INCLUDETEXT as follows

{ INCLUDETEXT  "c:\\temp\\LAP\\INTDLIFE.DOC" BookmarkName }

I tried this however it returned the message "Bookmark not found" or wording
similar to this.  This being because when the first document resolves into c:\
\temp\\LAP\\INTDLIFE.DOC, the bookmark is not preserved into that resolved
document.  I understand this is due to the bookmark name requiring to be
unique, however I have seen some code posted elsewhere that duplicates the
bookmark into the resolved document.  I just cannot figure out how to do this
though in my situation.

I have no direct control over the documents other than creating them.  The
system actually automates the production of the final document initiating all
the various steps of the mailmerge processes along the way.  I am hoping it
is possible to insert some VBA in the initial document containing the merge
field, so that the bookmark is preserved in its resolved document, so that
the document containing the INCLUDETEXT can find it.  Is this possible?

Many thanks

Stephen Lasham
Doug Robbins - Word MVP - 21 Aug 2007 03:33 GMT
' 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

For i = 1 To Result.Hyperlinks.Count

   linktarget = Result.Hyperlinks(i).SubAddress

   Set linkrange = Result.Hyperlinks(i).Range

   linkrange.Select

   linktarget = linktarget &
Format(Selection.Information(wdActiveEndSectionNumber))

   Result.Hyperlinks.Add Result.Hyperlinks(i).Range, "", linktarget

Next i

Source.Activate

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

   Loop

End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I am using a system that interfaces with MS/Word.  The system allows me to
> create some mailmerge documents.
[quoted text clipped - 50 lines]
>
> Stephen Lasham
Slash - 21 Aug 2007 04:14 GMT
Thanks, this is the same Macro as I had found in another thread, but it
doesn't have a macro name to it, and I do not know how to cause this to auto
run when the application I mentioned does all the mailmerge stuff.  I am
guessing this needs to be encompassed in a function or subroutine, with a
begin sub and end sub statement, and that this will be a special subroutine
that word recongises to auto run, is this the case?

>' Macro created by Doug Robbins to "preserve" bookmarks during a MailMerge
>
[quoted text clipped - 107 lines]
>>
>> Stephen Lasham
Slash - 21 Aug 2007 04:19 GMT
The above routine also appears to handle in the middle, the creation of the
new document, but this is done by the application I referred to.  So I am
guessing this script isn't going to work for me.  Would you agree?

>' Macro created by Doug Robbins to "preserve" bookmarks during a MailMerge
>
[quoted text clipped - 107 lines]
>>
>> Stephen Lasham
Doug Robbins - Word MVP - 21 Aug 2007 09:38 GMT
I doubt that you should be using mailmerge for this application.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> The above routine also appears to handle in the middle, the creation of
> the
[quoted text clipped - 114 lines]
>>>
>>> Stephen Lasham
Peter Jamieson - 21 Aug 2007 09:52 GMT
This makes it pretty hard to do.

The only way I can think of is to use Word's MailMerge Events to alter the
output document before the calling process saves it. It is certainly worth a
try.

In your Mail Merge Main Document you will need
a. to add a Class Module, e.g. in the VBA Editor, with the Mail Merge Main
document open, use Insert->Class Module
b. In the module's properties (you may see these in a window at the bottom
left) change the module name to ECM (short for Event Class Module)
c. In that module, insert the following code:

Public WithEvents App As Word.Application

Private Sub App_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult
As Document)
ActiveDocument.Range(Start:=0, End:=ActiveDocument.Range.End -
1).Bookmarks.Add "bkmbody"
End Sub

 d. Create an ordinary module (e.g. Insert->Module and insert the following
code:

Public X As ECM
Sub Register_Event_Handler()
 Set X = New ECM
 Set X.App = Word.Application
End Sub

Sub Unregister_Event_Handler()
 Set X.App = Nothing
 Set X = Nothing
End Sub

Sub autoopen()
Call Register_Event_Handler
End Sub

Sub autoclose()
Call Unregister_Event_Handler
End Sub

e. save and close the Mail Merge Main document.

When it is opened, the mail merge events should be enabled , so that when
the calling process finishes merging, the bookmark should be set.

However, be aware that
1. the calling applicaiton needs to be able to open documents that contain
macros without a dialog popping up
2. the calling applicaiton probably needs to /close/ the document for the
application-wide event handler to close down properly
3. you may need to tweak the event handling code to deal with, e.g. a
situaition where the merge fails, etc. In other words, this is just a
starting point.

Signature

Peter Jamieson
http://tips.pjmsn.me.uk

>I am using a system that interfaces with MS/Word.  The system allows me to
> create some mailmerge documents.
[quoted text clipped - 50 lines]
>
> Stephen Lasham
Peter Jamieson - 21 Aug 2007 10:05 GMT
> This makes it pretty hard to do.

By "this" I meant:

>> I have no direct control over the documents other than creating them.
>> The
>> system actually automates the production of the final document initiating
>> all
>> the various steps of the mailmerge processes along the way.

Signature

Peter Jamieson
http://tips.pjmsn.me.uk

> This makes it pretty hard to do.
>
[quoted text clipped - 111 lines]
>>
>> Stephen Lasham
Slash - 21 Aug 2007 23:50 GMT
Thanks for this, it seems the application is using MS/Word VBA code behind
the scenes as when I added this in it caused the VBA editor to pop up with
all the applications code showing, very interesting.  If I now look at the
two sets of solutions you have provided for me I may be able to see where I
can adjust the suppliers code, and then send them though some changes.

Thank you so much for the time you have given, it is greatly appreciated.

Stephen

>> This makes it pretty hard to do.
>
[quoted text clipped - 11 lines]
>>>
>>> Stephen Lasham
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.