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