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
'
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
--
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
"Slash" <u36791@uwe> wrote in message news:76ff84b94c64f@uwe...
>>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
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-mailmerge/200708/1
>>I am using a system that interfaces with MS/Word. The system allows me to
>> create some mailmerge documents.
--
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
"Slash via OfficeKB.com" <u36791@uwe> wrote in message
news:7701c3c1788b0@uwe...
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.
--
Peter Jamieson
http://tips.pjmsn.me.uk
"Slash" <u36791@uwe> wrote in message news:76ff84b94c64f@uwe...
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.
--
Peter Jamieson
http://tips.pjmsn.me.uk
"Peter Jamieson" <p...@KillmapSpjjnet.demon.co.uk> wrote in message
news:uFJStD94...@TK2MSFTNGP05.phx.gbl...
Thank you so much for the time you have given, it is greatly appreciated.
Stephen
Peter Jamieson wrote:
>> 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.
>
>> This makes it pretty hard to do.
>>
>[quoted text clipped - 111 lines]