Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2007 mail merge verify if file exists

Status
Not open for further replies.

Bluejay07

Programmer
Mar 9, 2007
780
CA
Hello,

I have a mail merge function that works great except for this one case.
The document checks a merge field (Y/N values) and uses INCLUDETEXT based on the result.

The odd occasion, a file doesn't exist which causes a field calcuation error and "Error! Not a valid filename." appears in the area for INCLUDETEXT.

I would like to verify the Y/N value and if there is a Y, then I want to check if the file exists. If the file doesn't exist, nothing should happen. If it does, the file is included.

Here is a sample of the existing code:
Code:
{IF {MERGEFIELD MULTIPLE}="N" "" "
...
{INCLUDETEXT "O:\\...\\...\\name{MERGEFIELD NUM}.dbf" \* CHARFORMAT}"}

Based on the following site, I tried to compare the error message.

While trying to keep things simple, I'm just trying to get the error comparison working first (before adding the Y/N check):
Code:
{IF {INCLUDETEXT "O:\\...\\...\\name{MERGEFIELD NUM}.dbf"}="Error! Not a valid filename" "" "
...
{INCLUDETEXT "O:\\...\\...\\name{MERGEFIELD NUM}.dbf" \* CHARFORMAT}"}

If I toggle the field codes for the IF INCLUDETEXT section, the result is:
{IF Error! Not a valid filename. = "Error! Not a valid filename." "" "
...

This is not working as I had hoped.
Any suggestions on how I can accomplish this?

Thanks.

If at first you don't succeed, then sky diving wasn't meant for you!
 
The basic error in your field code is that you're lacking an essential space character. Instead of:
name{MERGEFIELD NUM}.dbf"}="Error
you should as a minimum have:
name{MERGEFIELD NUM}.dbf"}= "Error

For an alternative construction, try:
{IF{INCLUDETEXT "O:\\...\\...\\name{MERGEFIELD NUM}.dbf"}<> "Error!*" {INCLUDETEXT "O:\\...\\...\\name{MERGEFIELD NUM}.dbf" \* CHARFORMAT}}

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

Thanks for the reply.
Looking at the code, I did have a space after the equal sign. I'm guessing I missed that when I was typing it (I couldn't paste).
Thanks. I'll be sure to keep an eye on that for the future.

Trying you're modified code seems to help.
The mail merge document is not giving an error if the file is not available.

I am still encountering some issues.

Here is a bit more background on the process.
A script is run from a xxx.bat file.
The script opens the original word document, runs a VBA macro that activates the mail merge and sends it to a new document.

When the new document is opened, it also opens a second document titled Mail Merge Errors1 which lists the records that have the field calculation error (in other words, the records where the INCLUDETEXT file does not exist). After I close the errors document, I see the mail merged document although some fields (date and some merge fields) have not been updated.

Any suggestion on how to eliminate the Errors page from appearing?

Thanks.



If at first you don't succeed, then sky diving wasn't meant for you!
 
If {MERGEFIELD NUM} returns a number, you could:
1. Create an empty document named 0.dbf
2. Merely use {INCLUDETEXT "O:\\...\\...\\name{MERGEFIELD NUM \# 0}.dbf" \* CHARFORMAT}
(Note the addition of the '\# 0')

As for your statement that "some fields (date and some merge fields) have not been updated", that suggests there is something else going on. A mailmerge ordinarily leaves no mergefields in the output. Are you sure the fields always exist in the data source? Date fields are auto-updating, so I can't see how they wouldn't update.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top