PDA

View Full Version : Problem with DocVariables and IF statement



DavG63
07-31-2014, 12:25 PM
Hi

Please help me I think I'm losing my mind!

I have a DocVariable in my word document which, only shows a value when the document is referring to a Company, i.e. if referring to an individual or a partnership it is not used.

I have tried every combination I can come up with to make the field report a blank space in the event of the inevitable Error! No document variable supplied. I managed to get it to do it when I'm loading a data file for an individual, but the next time I go to do one for a Company, and TextBox1 into which I type the Company Number has been completed on the Userform, it still reports a blank space on the document rather than importing the value of Textbox1.

My code for the DocVariable is
IF DocVariable Var3 = "Error! No document variable supplied" "" DocVariable Var3

What am I doing wrong? Please tell me!

macropod
07-31-2014, 03:34 PM
Try:
{IF{DocVariable Var3}= "Error!*" "" {DocVariable Var3}}

Note: The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message.

gmaxey
07-31-2014, 03:39 PM
You could use this: {If { DocVariable Var3 } = "Error! No document variable supplied."""{ DocVariable Var3 }}

Are you updating the field after setting the variable?

Sub ScratchMacro()
ActiveDocument.Variables("Var3").Value = "Acme Hardware"
ActiveDocument.Fields.Update
ActiveDocument.Variables("Var3").Value = "Wylie Coyote"
ActiveDocument.Fields.Update
ActiveDocument.Variables("Var3").Value = ""
ActiveDocument.Fields.Update
End Sub

DavG63
07-31-2014, 11:14 PM
Try:
{IF{DocVariable Var3}= "Error!*" "" {DocVariable Var3}}

Note: The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message.

Macropod! It worked! Thanks so much, I was on the verge of giving it up as a bad job. Greg - thanks for responding too, unfortunately I don't know enough about VBA to be able to tell whether I'm updating the field after setting the variable! if it helps, I have a combobox with three options in it "company", "individual" or "partnership". I've defined a variable called Company which loads in the address details for each, i.e. "a company incorporated under the Companies Acts" or "residing at" or "having a place of business at" and I have an IF within the Combobox to say if it changes and it equals one of the choices, then Company becomes on of the various choices. Once the user clicks 'ok' and unloads the form, I assume that's when the field is updated but to be honest I'm not 100% sure of that either!

All I know is Macropod's suggestion works and that's good enough for me.

Thanks again for your help.