Share via

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

Anonymous
2022-01-11T16:08:16+00:00

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.

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2022-01-13T02:19:42+00:00

    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.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-01-12T09:31:18+00:00

    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 answer helpful?

    0 comments No comments
  3. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2022-01-12T01:26:47+00:00

    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).

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2022-01-12T00:13:24+00:00

    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

    Was this answer helpful?

    0 comments No comments
  5. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2022-01-11T17:41:08+00:00

    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.

    Was this answer helpful?

    0 comments No comments