View Single Post
  #1   Report Post  
JR042 JR042 is offline
Junior Member
 
Posts: 1
Default Calculations with possible null value fields

I'm trying to perform the following calculation in Word 2007 mailmerge, using a txt file (with line return record delimiters and vertical bar | field delimiters) as my data source but I keep getting a syntax error if any of the fields have null values:
R1 + C1 + M1

I can use IF statements to test which fields have null values first, and then only perform the relevant calculation on those that don't have null values but when the number of fields in the calculation is higher than two, the number of permutations gets a bit too complex. I may have to do this for a calculation involving 12 fields so I'd rather avoid this kind of logic:

If R1 AND C1 AND M1 = null, total = ‘0’
Else, If R1 AND C1 = null, total = M1
Else, If R1 AND M1 = null, total = C1
Else, If R1 = null, total = C1 + M1
Else, If C1 AND M1 = null, total = R1
Else, If C1 = null, total = R1 + M1
Else, If M1 = null, total = R1 + C1
Else, total = R1 + C1 + M1

If I use Excel as the data source, somehow Word seems to assume null = 0 and the calculation works without IFs, but I have no choice but to use a txt file (there are more than 255 fields, which I believe is the max you can have in Excel - there may be other reasons for using txt which I don't know as I'm not responsible for it and would have difficulty having this changed). I can't manipulate the data source myself but I am looking at having the null values changed to zeros, if possible.

Failing that, is there a simpler way to do this? I've tried to simplify the above by showing only the logic, rather than the actual code, but I'm quite new to programming so if there is a better solution, please could you show the full code?

Thanks!