DocVariable Problem

S

singeredel

I have a field code of DocVariable Quadis. Can anyone tell me why the
following code still displays an "Error! No document variable supplied" when
the variable QuadisNo$ is blank?

If QuadisNo$ = "" Then
ActiveDocument.Variables.Add Name:="Quadis", Value:=""
Else: ActiveDocument.Variables.Add Name:="Quadis", Value:="DocID " +
QuadisNo$
End If
ActiveDocument.Fields.Update

I just need fields to display nothing if the variable is blank.

Thanks...
 
J

Jonathan West

singeredel said:
I have a field code of DocVariable Quadis. Can anyone tell me why the
following code still displays an "Error! No document variable supplied"
when
the variable QuadisNo$ is blank?

If QuadisNo$ = "" Then
ActiveDocument.Variables.Add Name:="Quadis", Value:=""
Else: ActiveDocument.Variables.Add Name:="Quadis", Value:="DocID "
+
QuadisNo$
End If
ActiveDocument.Fields.Update

I just need fields to display nothing if the variable is blank.


Its the way Variables work - if they are blank, they are deleted altogether.
Put a single space in instead.
 
J

Jay Freedman

Setting the value of any document variable to an empty string "" is exactly
the same as deleting it -- so even after your .Variables.Add statement, the
variable Quadis does not exist in the document. That's what the field result
is telling you.

You can insert a space character as the value of the variable, Value:=" ",
and the field will display a space. This should be ok unless something else
follows the field on the same line in the document.
 
G

Greg

Instead of setting your Variable Quadis to "" set it to " " then in
your Field code use

{ IF {DocVariable "Quadis" } = " """{DocVariable "Quadis"}}

I
 
H

Helmut Weber

Hi Julie,

working with fields isn't real programming from
my point of view, so I can help you only with
explaining what is going on:

MsgBox ActiveDocument.Variables.Count ' e.g. 5
ActiveDocument.Variables.Add Name:="Quadis", Value:=""
MsgBox ActiveDocument.Variables.Count ' still 5
so no variable was created.

Maybe microsoft.public.word.docmanagement
would be better suited for such a question.

Though there is sufficient knowledge here,
I guess, but unfortunately not by me.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
I have a field code of DocVariable Quadis. Can anyone tell me why the
following code still displays an "Error! No document variable
supplied" when the variable QuadisNo$ is blank?

If QuadisNo$ = "" Then
ActiveDocument.Variables.Add Name:="Quadis", Value:=""
Else: ActiveDocument.Variables.Add Name:="Quadis",
Value:="DocID " + QuadisNo$
End If
ActiveDocument.Fields.Update

I just need fields to display nothing if the variable is blank.

Try:
'_______________________________________
Sub test()
Const VariableName As String = "Quadis"
Dim QuadisNo As String

QuadisNo = ""

ActiveDocument.Variables(VariableName).Value = "Temp"

If QuadisNo = "" Then
ActiveDocument.Variables(VariableName).Value = ""
Else
ActiveDocument.Variables(VariableName).Value = "DocID " & QuadisNo
End If
ActiveDocument.Fields.Update

End Sub
'_______________________________________

I think that if you use something like:
ActiveDocument.Variables(VariableName).Value = ""
twice in a row (It could happen depending how often your code is run), then
the first time you set the variable to zero length and delete it, but the
fields seems to be able to handle that. If you do it a second time, then the
field displays the error message.

Another way around that is:
'_______________________________________
Sub test()
Const VariableName As String = "Quadis"
Dim QuadisNo As String

QuadisNo = ""


If QuadisNo = "" Then
ActiveDocument.Variables(VariableName).Value = " "
Else
ActiveDocument.Variables(VariableName).Value = "DocID " & QuadisNo
End If
ActiveDocument.Fields.Update

End Sub
'_______________________________________

But here you will get a space in lieu of the DOCVARIABLE field in the
document, and I don't know if this is acceptable in your case.

I am sure someone will be along shortly to explain the fine intricacies of
the DOCVARIABLE field!
..
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top