Bug with nested Docvariables updating?

Hi,
I think there's a bug in the way that nested docvariable fields update. I'm posting this here in the hopes that someone has found a way around this (the question is about fields, but I'm using VBA to manipulate them, so I honestly don't know which forum is best. Fingers crossed.)

The issue (I think) is that a Docvariable field can't be updated if it is not (or would not) be evaluated by the larger expression. In general practice this is probably not a big deal, but I'm trying to check that fields haven't been typed over, and it is a problem when the value of the docvariable doesn't match its result. 

Create three document variables

- In the VBE Immediate Window:

activedocument.Variables("Var1").Value = "dog"
activedocument.Variables("bVar1").Value = "true"

Create some fields

- In the document (all brackets should be created using Ctrl+F9:
{DOCVARIABLE  Var1  \* MERGEFORMAT}

{IF {DOCVARIABLE  Var1} = "dog" "Found a dog!"}

{IF {COMPARE {DOCVARIABLE  bVar1} = "true"} = 1 {DOCVARIABLE  Var1} {IF {DOCVARIABLE  Var1} = "giraffe" "giraffe"} }

Update all fields and view their results. You should see:
dog
Found a dog!
dog

As expected. Now, change the value of Var1 to "rabbit":
activedocument.Variables("Var1").Value = "rabbit"

Update all fields and view their results. You should see:
rabbit

rabbit

Toggle field codes, select the very last DOCVARIABLE  Var1 field and toggle only it. It is still "dog".

Even if you select that field and update it specifically (rather than as part of a larger selection), it remains "dog".

You can see the problem as well by changing bVar1 to "false" and then changing Var1 again. Now the second-to-last DOCVARIABLE  Var1 field will be outdated.

I may be able to work out some logic for ignoring fields that don't directly impact the displayed value in my check. But that seems like a long way to go for what should be a relatively simple process.

Can anyone propose an alternate solution? Or perhaps replicate/not replicate my results? 

Thanks!

I cannot replicate the issue (as I cannot get the last Docvariable Var1 field to display anything when I toggle it.

 

However, if I change the last construction to

 

{IF {COMPARE {DOCVARIABLE bVar1} = "true"} = 0 {DOCVARIABLE Var1} {IF {DOCVARIABLE Var1} = "rabbit" "giraffe"} }

 

it shows giraffe, indicating that the last docvariable var1 field is returning rabbit.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I don't see the failure of the nested DOCVARIABLE field to update. What I do see that's a bit unexpected is that when bVal1 = "true", the {DOCVARIABLE Var1} field just before the = "giraffe" appears to have an empty string as its value. However, the preceding one contains the correct current value of Var1.

When I change bVar1 to "false", both of those {DOCVARIABLE Var1} fields have the correct value.

Either way, the displayed result of the outer IF field is correct.

I think what we're seeing is the result of something called "short-circuit evaluation": When the result of the IF statement's comparison is True, then the False clause is not evaluated at all. When the comparison is False, the True clause may or may not be evaluated. This optimization saves time and CPU cycles.


For a workaround to verify that fields haven't been deleted or overwritten, you could compare the field's .Code property instead of its result:

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hmm, do you mean that the result of the entire field is giraffe? In that case bVar1 must be false, meaning that the IF statement is being evaluated and the field would therefore update. The reason you don't see anything when you toggle initially is because the field won't update. It's empty.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Right, the displayed result is correct because the evaluated fields update. Initially you see an empty value because the field is empty. When you change bVar1 to false, the first Var1 holds its prior value, and the second one updates because it will be evaluated. If you change Var1 again you'd see that the first Var1 becomes outdated (if I have this all right).

Your idea of testing the outer code is interesting, but since only one of the codes will ever update I think it poses the same problem. I just find it incredibly odd that I can't force the fields to update in such a case. I don't like having outdated values running around in my documents.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I replaced the 1 by 0

 

If the condition in the outer If field is false, then the final docvariable field is evaluated as it is part of the false result for the outer If field.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Right. I would expect that. It's the variable that's not being evaluated that also can't be updated.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 647 Applies to: