Word 2007 - if DOCVARIABLE is blank

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Word 2007 - if DOCVARIABLE is blank

Post by diana »

Im working with docvariable fieldcodes.

Ive the following syntax

{ IF { DOCVARIABLE VarName } = "Number" "True" "False" }

however no matter how I set my docVariable its not displaying the results I want.

I want:
If {docVariable name = blank/empty "display nothing" "display the value"}

well ive actually done it the other way:
If {docVariable name <> blank/empty "display the value" "display nothing"}

my docVariable field code is:
{ if {DOCVARIABLE cms_contact_position } <> "" "{ DOCVARIABLE cms_contact_position \* charformat }" ""}


any ideas?

TIA, dd

User avatar
HansV
Administrator
Posts: 78463
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word 2007 - if DOCVARIABLE is blank

Post by HansV »

A document variable cannot be blank. If you set the value of a document variable to "" in VBA, the document variable is removed.
The best you can do is set the value to a single space:

Code: Select all

ActiveDocument.Variables("VarName") = " "
You can then use a field code like this in the document:

Code: Select all

{ IF { DOCVARIABLE "VarName" }=" " "" "{ DOCVARIABLE "VarName" }" }
Best wishes,
Hans

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Re: Word 2007 - if DOCVARIABLE is blank

Post by diana »

Thanks Hans

yes I had a feeling there was a limitation with DocVariable = blank or ""

Many thanks again :clapping:

dd