Word VBA Bookmark Stops Merge

O

OriRefugee

I'm AB-SO-LUTE-LY stumped, banging my head against the wall, and am
begging (BEGGING, I SAY) for help!!!!!!

I created a dynamic Word Mail Merge letter with Bookmarks but,
regardless of the size of my data file, ONLY two records are merged
(regardless of where I start in the data file).

I'm using Word 2002, standard "WritetoBookmark" sub (see below) called
from the "WdApp_MailMergeBeforeRecordMerge" event of a class.
Everything (besides this) looks like it's behaving correctly. All
records merge when the call to this sub is commented out, uncomment
it, BAM -- only two records are merged. Originally started with > 1
bookmark, but pared everything down to troubleshoot -- tried
everything and am COMPLETELY perplexed. SEND HELP!
-----------------------------------------------------------

Sub WriteToBookmark(doc As Word.Document, _
bkmName As String, bkmValue As String)
Dim rng As Word.Range

Set rng = doc.Bookmarks(bkmName).Range
rng.Text = bkmValue
doc.Bookmarks.Add Name:=bkmName, Range:=rng
Exit Sub
End Sub
 
P

Peter Jamieson

I tried your code (with and without the Exit Sub) here with a very simple
..doc data source (2 tab-seprated columns and 6 data rows) and WOrd 2002
SP-2. Everything seemed fine. I assume that when you are testing you have
only one call to WriteToBookmark within WdApp_MailMergeBeforeRecordMerge

I'm not a VBA expert but I wonder whether the location of your sub makes any
difference. For testing, I put the two routines in the same module.
 
O

OriRefugee

Hmmm...I currently DON'T have them within the same module -- I'll give
that a try. Something else you mentioned gave me pause, too -- SP2,
huh? I have SP1 -- I'll get the service pack and see if that helps,
too!

(yeah, the exit sub was left over from error trapping I added at one
point to see if something was bombing without notifying me!)

Thanks for the input! I'll try those two things and see if it fixes
the problem!!
 
C

Cindy M -WordMVP-

Hi OriRefugee,

WHY are you using Exit Sub? I can't see any need for it at all...

And you should have your code check whether the bookmark still exists,
and only write to it if it does (If
ActiveDocument.Bookmarks.Exists(bkmName) Then). If it does not (Else)
display a message box so that you know this is happening.

I'd also set a breakpoint at the line that calls this procedure (click
in the line, then press F9). Code execution will stop everytime it
reaches this line. Press F8 to step through line by line, including into
the that follows the call to this procedure.

And if you're using On Error Resume Next, take that out so that you can
see what error message is being generated and stopping your code (I
suspect this may be what's going on).
I created a dynamic Word Mail Merge letter with Bookmarks but,
regardless of the size of my data file, ONLY two records are merged
(regardless of where I start in the data file).

I'm using Word 2002, standard "WritetoBookmark" sub (see below) called
from the "WdApp_MailMergeBeforeRecordMerge" event of a class.
Everything (besides this) looks like it's behaving correctly. All
records merge when the call to this sub is commented out, uncomment
it, BAM -- only two records are merged. Originally started with > 1
bookmark, but pared everything down to troubleshoot -- tried
everything and am COMPLETELY perplexed. SEND HELP!
-----------------------------------------------------------

Sub WriteToBookmark(doc As Word.Document, _
bkmName As String, bkmValue As String)
Dim rng As Word.Range

Set rng = doc.Bookmarks(bkmName).Range
rng.Text = bkmValue
doc.Bookmarks.Add Name:=bkmName, Range:=rng
Exit Sub
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
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 :)
 
O

OriRefugee

Thanks for the help!! I moved the code into the same module, even
updated to sp-2, still no go. I only have one call to the
WriteBookMark routine --

I removed EVERYTHING -- created a clean doc, added one merge field and
one bookmark. Created a data file with three records (comma-delmited
fields, CRLF-delimited records) -- still no go. Only 2 records merge
-- remove the call to WritetoBookmark -- Yup, all three merge.

My calling proc for my test letter looks like this:

Private Sub WdApp_MailMergeBeforeRecordMerge( _
ByVal doc As Document, Cancel As Boolean)

'Ok, let's customize the output for the recipient...
With doc.MailMerge.DataSource

WriteToBookmark doc, "DynamicInfo", "BlahBlah"
End With
End Sub


Pretty cut and dry, huh? But even this simple test won't work for
me!!!
ARGH!! Could I have a Word setting that is incorrect??
 
O

OriRefugee

Hi, Cindy!

Thanks so much for your help!! The "Exit Sub" was a leftover from
error checking -- I removed all error checking to ensure that, for
some reason, an error was being masked and causing execution to stop.

I've been breaking in the calling proc and walking through the code
-- and here's a BIG source of the frustration!! -- it JUST STOPS after
the second iteration. Query the Err object and it's empty -- looked
at the Locals window -- nothing looks out of sorts or weird or changed
between the first and second record -- nothing to stop it from
executing! ARGH!!! It's driving me CRAZY!

Checking for the presence of the bookmark -- it's there, life is
good -- everything LOOKS like it should process --

I found your web site and downloaded your zip file (merge events and
employees doc) and ran it without modification and GUESS WHAT??? Only
the first two records were processed --- (WHY ME?) -- could it be a
setting or environment cause? I have Word 2002 and upgraded to sp-2
and it still happens -- nothing looks out of sorts in my Word
settings. I can't find anything on the MS site -- ARGH! WHAT COULD
IT BE??
 
C

Cindy M -WordMVP-

Hi OriRefugee,
I've been breaking in the calling proc and walking through the code
-- and here's a BIG source of the frustration!! -- it JUST STOPS after
the second iteration.
OK, setting a break... and STEPPING through? If you STEP through (F8)
you should at least be able to determine on which line it stops? That
could give us a clue, even if you don't get an ERR. I've seen this
happen on occasion with Word VBA, and it always turns out to be
something "incompatible" between the code and the current situation in
the actual document that Word simply can't resolve.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
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 :)
 
O

OriRefugee

OK, setting a break... and STEPPING through? If you STEP through (F8)
you should at least be able to determine on which line it stops? That
could give us a clue, even if you don't get an ERR. I've seen this
happen on occasion with Word VBA, and it always turns out to be
something "incompatible" between the code and the current situation in
the actual document that Word simply can't resolve.

Yes, that's the confusing part -- it completes the "before record
merge subroutine" -- it doesn't stop mid subroutine like you would
expect when an error is generated, but processes line-by-line until
the end.

I ran your example (merge events and employees) again and here are the
chain of events as I stepped through everything:

appWd_MailMergeBeforeRecordMerge
Gets initial "N."
Puts value in Bookmark
appWd_MailMergeAfterRecordMerge
Debug Prints "Record 1: Davolio"
appWd_MailMergeBeforeRecordMerge
Gets initial "A."
Puts value in Bookmark
appWd_MailMergeAfterRecordMerge
Debug Prints "Record 2: Fuller"

IMMEDIATELY goes to appWd_MailMergeAfterMerge (??? doesn't
go to the next record)
msgbox displayed

Querying the doc.mailmerge.datasource.activerecord is 1 again -- it
never moves to 3.

When you saw this type of thing before with VBA, what types of things
were incompatible?? I'll gladly track it down and see if it's my
problem -- I'm so frustrated. I tried it on another machine in the
dept with the same results.

Thanks so much, Cindy, for your time with this -- I'm truly at my
wit's end and I so appreciate your help with this!
 
C

Cindy M -WordMVP-

Hi OriRefugee,
Thanks so much, Cindy, for your time with this -- I'm truly at my
wit's end and I so appreciate your help with this!
I just wish light bulbs were going off above my head...
When you saw this type of thing before with VBA, what types of things
were incompatible??
Things like the cursor being somewhere that wouldn't let a command
execute, for example.

Silly question: you are certain you've not set a filter on the records?

Could you zip up the "pared to the bone" test files (not Northwind, I
have that) and email them to (e-mail address removed) ? I'll see if
they run here - that would at least tell us if it's a problem with your
configuration.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
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