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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the value of a Word Mail Merge Datafield 2

Status
Not open for further replies.

iandavies147

Technical User
Apr 8, 2008
3
GB
Hi

Is it possible to add a condition to a word document so that you dont display a word mail merge datafield if it has a certain string contained in the datafield for example a comma?

strValue = ActiveDocument.MailMerge.DataSource.DataFields("CUSTOMER")
if instr(strValue,",") > 0 then
ActiveDocument.MailMerge.DataSource.DataFields("CUSTOMER") = ""
end if


This code generates an error reporting that it is an "improper use of property", I know you can add conditions in the code that use mergfields but I can't see how these can be used with instr?

e.g. {IF {MERGEFIELD CUSTOMER} = "UK" "British" "Not British"}


 
At first I would have bluntly answered "no" as you are in a document and there is no Word-field even resembling INSTR. But I suggest you take a look at the COMPARE field.

German Word 2000 Help says, {COMPARE "{MERGEFIELD Postleitzahl}" = "985*"} would show if 'Postleitzahl' is between 98500 and 98599. So Word seems to regard the asterisk not as a character (as in this context could be expected) but as a wildcard - although the asterisk is inside the quotes.

By playing around you maybe could adapt your IF condition so the COMPARE field suits your needs.

HTH a little.
 
Hi iandavies147,

You can test for the presence of a comma, but it helps if you know where it might appear in the field. For example, to test for a comma in the 2nd character position, you could use:
{IF{MERGEFIELD CUSTOMER}<> "?,*" {MERGEFIELD CUSTOMER}}

You can build this up to test as many character positions as you might need, using:
{IF{MERGEFIELD CUSTOMER}<> ",*" {IF{MERGEFIELD CUSTOMER}<> "?,*" {IF{MERGEFIELD CUSTOMER}<> "??,*" {IF{MERGEFIELD CUSTOMER}<> "???,*" {IF{MERGEFIELD CUSTOMER}<> "????,*" {MERGEFIELD CUSTOMER}}}}}}
You can nest up to 20 IF tests this way. You can replace the comma with a multi-character string if you like.

The above solution takes advantage of the fact that both asterisks and question marks are treated as wildcards. Unfortunately, though, you can't use an expression like:
{IF{MERGEFIELD CUSTOMER}<> "*,*" {MERGEFIELD CUSTOMER}}

Cheers

[MS MVP - Word]
 
Although both the previous posts are useful unfortunately they will not allow me to resolve this particular issue. The problem I have is that the comma can appear in any of the first 30 character positions

Is there no way of setting the value of a datasource field to reset the value if a comma exist in the field value, this way I could use a macro to reset the field value.

ActiveDocument.MailMerge.DataSource.DataFields("CUSTOMER") = ""
 
Hi iandavies147,

The solution I posted CAN work for 30 possible comma positions - all you need is to use two sets of IF tests - one for the first 15 characters and another for the second 15 (or any other mix with no more than 20 nested IF tests).

Cheers

[MS MVP - Word]
 
Hi macropod,

Sorry to be a burden but I can't easily see how the use of two sets of IF tests can be used to resolve my issue.

In the sample code you gave above you are testing for positions 1,2,3 and 4 could you show me the same tests but using two IF tests spliting the first IF for conditions 1 and 2 and the second IF for positions 3 and 4.

The problem I have is that I only want to display the CUSTOMER field if the comma dosn't appear in any of the positions and by entering 2 IF statements I would presume that one of the IF statements would always fail and therefore print the field?

Thanks in advance for any help you can give.

 
Hi iandavies147,

Here's one way - code the first set of fields like:
{IF{MERGEFIELD CUSTOMER}<> ",*" {IF{MERGEFIELD CUSTOMER}<> "?,*" {IF{MERGEFIELD CUSTOMER}<> "??,*" {IF{MERGEFIELD CUSTOMER}<> "???,*" {IF{MERGEFIELD CUSTOMER}<> "????,*" {SET CUSTOMER 1} {SET CUSTOMER 0}}}}}}
Note the pair of True/False SET statements at the end.

Then code the second set like:
{IF{CUSTOMER}= 1 {IF{MERGEFIELD CUSTOMER}<> "???????????,*" {IF{MERGEFIELD CUSTOMER}<> "????????????,*" {IF{MERGEFIELD CUSTOMER}<> "?????????????,*" {IF{MERGEFIELD CUSTOMER}<> "??????????????,*" {MERGEFIELD CUSTOMER}}}}}}

Cheers

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

Part and Inventory Search

Sponsor

Back
Top