Document Variables - if the value in one document variable equals another then null one of the document variables.

Hi,

I would like to create an if statement in MS Word to compare the values in two document variable and if the values match then null/empty one the document variable values. Is this possible?

Example

If "{DOCVARIABLE Name1}" = "{DOCVARIABLE Name2}" .... remove one of the values.

This isn't possible using fields in the body of the document. You can test whether the values are the same, but fields don't have any way to delete a document variable.

You can do this with a macro:

Sub test()

With ActiveDocument

If .Variables("Name1").Value = .Variables("Name2") Then

.Variables("Name2").Delete

.Fields.Update

End If

End With

End Sub

The update would then result in any {DOCVARIABLE Name2} fields returning this display:

Image

For completeness: Setting ActiveDocument.Variables("Name2").Value = "" will achieve the same result.

_____________________________
https://jay-freedman.info

2 people found this reply helpful

·

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.

Hi Jay,

A document variable cannot have a null ("") value. Therefore, it would be necessary to use

ActiveDocument.Variables("Name2").Value = " "

with a space between the quotes.

@ Mark Sullivan - It is not really clear what you are doing, but possibly you could use

{ IF { DOCVARIABLE Name1 } <> { DOCVARIABLE Name2 } {DOCVARIABLE Name2 } }

Which will only display the value of the variable Name2 if it is not the same as the value of the variable Name1

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

1 person found this reply helpful

·

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.

What I was trying to say is that setting a document variable's value to an empty string, "" , causes the variable to be deleted, so that's equivalent to calling its .Delete method. Setting the value to a space character does not delete the variable ("remove" it, in Mark's words).

_____________________________
https://jay-freedman.info

2 people found this reply helpful

·

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.

Hi Doug,

Thanks for your feedback.

My document variables contain address lines and on occassion the address lines can be identical. I want to remove the duplicate address line when comparing the two fields.

I hope it makes the requirement clearer :).

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.

Use an If...then...Else field construction such as the following in which the paragraph containing the Address2 detail will only appear if it is different from the Address1 detail 

Note the Enter key has been pressed where the pilcrow shaded in yellow appears.

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

1 person found this reply helpful

·

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 February 23, 2023 Views 641 Applies to: