Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Walter Briscoe Walter Briscoe is offline
external usenet poster
 
Posts: 42
Default Handling errors in merge VBA

I run Word and Excel 2003. I have a VBA macro which mailmerges an Excel
file into a Word file. Usually, a new file window is created and gets
focus. My macro then continues and does some post processing.
Yesterday, I found that an error window containing "A field calculation
error occurred in record 1." was also created and got the focus.
This got me thinking. I have a couple of questions for the experts he

1) Do mailmerge error messages all get tagged "Error!"?
Mine is "Error! Bookmark not defined" and should be easy to fix. If the
tag is mere coincidence, then going from the error window to the
appropriate part of the output document would be hard.

2) How do I get my VBA to recognise that an error file was output and
took focus and force focus to proceed to the output data window rather
than the error window?
--
Walter Briscoe
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Handling errors in merge VBA

The following message I wrote a couple of years ago doesn't answer all
your questions but I'm posting it in full anyway because I suspect most
of it will be relevant.

Sorry, I cannot remember (and probably never knew) whether all the error
messages are prefixed by "Error!"


As far as I know because I don't do bulk stuff like this,
a. you cannot trap all errors using the Word object model. For example, you
cannot trap the "error" where the Mail Merge Data Source does not exist.
b. You probably already know that you can reduce the possible number of
dialog messages by setting

objWordApplication.DisplayAlerts = wdAlertsNone

(where objWordApplication is your Word Applicaiton object

c. You can suppress some types of error by executing the merge using

objWordApplication.objMMMD.MailMerge.Execute Pause:=False

(rather than objWordApplication.objMMMD.MailMerge.Execute Pause:=True)

where objMMMD is your Mail Merge Main Document object

If you do that, some syntax errors will be logged in a separate Errors
document. But how do you know that such a document has been created? Well,
you can compare the document count and document names pre- and post- merge.
For example, if pre-merge your document count is always 1, then
1. if you are merging to a new document and there are no errors, post-merge
you should see 2 documents (MMMD, destination document), and the
ActiveDocument should be the Destination Document
2. if you are merging to a new document and there are errors, post-merge
you should see 3 documents (MMMD, destination document, error document), the
ActiveDocument should be the error document, and the ActiveDocument.Name
should be something like "Mail Merge ErrorsNNN" where NNN is a sequence
number (at least, in Word 2003, English Language version it's like that).
3. if you are merging to any other destination type and there are no
errors, post-merge you should see 1 document (MMMD), and the ActiveDocument
should be the MMMD
4. if you are merging to a any other destination type and there are errors,
post-merge you should see 2 documents (MMMD, error document), the
ActiveDocument should be the error document, and the ActiveDocument.Name
should be as above.

I know that some simple errors, e.g. you have a formula field { ={
MERGEFIELD myfield } } and myfield is not numeric) will be dealt with by
setting .DisplayAlerts = wdAlertsNone anyway (in other words, if those are
the only types of error you get, you don't need to contemplate produing an
Error Document, but if you do, an Error Document will be produced when this
error occurs, regardless of the state of .DisplayAlerts). However, I don't
have enough experience of doing that to tell you exactly what types of error
are trapped by what technique.


Peter Jamieson

http://tips.pjmsn.me.uk

Walter Briscoe wrote:
I run Word and Excel 2003. I have a VBA macro which mailmerges an Excel
file into a Word file. Usually, a new file window is created and gets
focus. My macro then continues and does some post processing.
Yesterday, I found that an error window containing "A field calculation
error occurred in record 1." was also created and got the focus.
This got me thinking. I have a couple of questions for the experts he

1) Do mailmerge error messages all get tagged "Error!"?
Mine is "Error! Bookmark not defined" and should be easy to fix. If the
tag is mere coincidence, then going from the error window to the
appropriate part of the output document would be hard.

2) How do I get my VBA to recognise that an error file was output and
took focus and force focus to proceed to the output data window rather
than the error window?

  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Walter Briscoe Walter Briscoe is offline
external usenet poster
 
Posts: 42
Default Handling errors in merge VBA

In message of Thu, 28 May 2009
10:08:22 in microsoft.public.word.mailmerge.fields, Peter Jamieson
writes
The following message I wrote a couple of years ago doesn't answer all
your questions but I'm posting it in full anyway because I suspect most
of it will be relevant.

Sorry, I cannot remember (and probably never knew) whether all the
error messages are prefixed by "Error!"


[snip]

It is good stuff and led me to write the following naive code:
' Put focus on output
If Documents.Count 2 Then
MsgBox "something went wrong with merge, will focus on output"
ActiveDocument.Close
End If

I probably ought to put the focus on documents(2), but Rome wasn't built
in a day. Windows(Documents(2).name)).activate might do it - but did not
immediately occur to me.

The cause of the failure entertains me:
I have some script:
{if 1 = 1 {INCLUDETEXT "{DOCVARIABLE Home}enc.doc" "{MERGEFIELD foo}_a_b"}}}
(I apologise if I made any syntax errors, I transcribed by hand. I never
noted the code to copy the text of a field rather than its result.)
This pulls in 2 alternative bookmarks no_a_b or yes_a_b according to foo
being no or yes. You've guessed it; it was neither.
I thing I would have to write something brutal to fix the heads, tails,
on the side problem. Meanwhile, I will live with it and check the
specification of the data.
--
Walter Briscoe
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL errors in mail merge projectmergeleader Mailmerge 10 September 7th 07 04:27 PM
Errors in Merge Document ReneeC Mailmerge 8 October 30th 06 11:37 AM
Merge - Encountering Errors Janazago Mailmerge 0 April 19th 06 05:31 PM
Mail Merge errors Hali Cleveland Mailmerge 2 December 12th 05 05:56 PM
Merge errors [email protected] Mailmerge 4 February 10th 05 12:17 AM


All times are GMT +1. The time now is 01:12 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"