> I cannot seem to find how to write VBA code to delete a DocVariable
> field in the ActiveDocument. I have a DocVariable field with the name
> of "Quadis" that I want to delete if a certain "If statement" is
> false.
>
> Thanks...
I see at least two points of confusion here. Please clarify what you're
asking for...
- Do you want to delete the {DocVariable} field from the body of the
document, or do you want to delete the document variable itself (which would
cause the field to display nothing when it's updated, although the field is
still there)?
- If it's the field you want to delete, then fields don't have names. Do you
mean that the name of the document variable is "Quadis" and the field code
is {DocVariable Quadis} ?
To delete a document variable, set its value to an empty string:
ActiveDocument.Variables("Quadis").Value = ""
To delete all DocVariable fields that refer to this variable, loop through
all fields and test their codes:
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If (oFld.Type = wdFieldDocVariable) And _
(InStr(oFld.Code, "Quadis") > 0) Then
oFld.Delete
End If
Next oFld

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
singeredel - 18 Feb 2005 01:49 GMT
Bless you! All of the fields will be unlinked once the document is created
using variables supplied by information from a dialogue box, so that it is
just a text document whose fields will never have to be updated again, so I
don't want anything to display in fields where there is no data. When I try
to run it with the DocVariable field with no variable supplied, it shows an
error message in the document, the text of which remains in the document when
the field is unlinked. I am just a layperson and really green at this and
there may be a better way to supply variables to a document, but I am not
aware of how to do it.
Thanks again...
> > I cannot seem to find how to write VBA code to delete a DocVariable
> > field in the ActiveDocument. I have a DocVariable field with the name
[quoted text clipped - 28 lines]
> End If
> Next oFld
ActiveDocument.Variables("Quadis").Delete
>I cannot seem to find how to write VBA code to delete a DocVariable field
>in
[quoted text clipped - 3 lines]
>
> Thanks...
singeredel - 18 Feb 2005 01:49 GMT
Thanks for your help. I had previously tried this statement and it gave me an
error.
> ActiveDocument.Variables("Quadis").Delete
>
[quoted text clipped - 5 lines]
> >
> > Thanks...
Jezebel - 18 Feb 2005 02:10 GMT
It will give an error if the variable isn't defined; not otherwise. Precede
it with On Error Resume Next -- then it doesn't matter.
> Thanks for your help. I had previously tried this statement and it gave me
> an
[quoted text clipped - 11 lines]
>> >
>> > Thanks...
Helmut Weber - 18 Feb 2005 11:51 GMT
Hi,
or set the value of the variable to "".
ActiveDocument.Variables("Quodis") = ""
That deletes the variable, if there is one,
otherwise does nothing.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
singeredel - 18 Feb 2005 15:47 GMT
Thank you...looks like that was the solution. :)
> Hi,
>
[quoted text clipped - 9 lines]
> Word XP, Win 98
> http://word.mvps.org/
singeredel - 18 Feb 2005 16:01 GMT
OK, I think I understand what the problem was. This is the code I had
written, which obviously doesn't assign a variable to "Quadis" when the "If"
statement is "No":
If optQuadisYes = True Then
ActiveDocument.Variables.Add Name:="Quadis", Value:="DocID " +
QuadisNo$
Else:
ActiveDocument.Variables("Quadis").Delete
End If
What I was attempting to do was delete the DocVariable field but could not
figure out how to do it. I didn't want to have to assign an Index number to
the code to delete the field because then I would have to keep track of all
the field numbers to know what to reference. I didn't realize the name
"Quadis" assigned to the DocVariable could not be used to identify the field
for deletion. In this case, I guess the best solution would be to not delete
the field but just give it a value of nothing:
ActiveDocument.Variables("Quadis").Value = ""
Thank you
> It will give an error if the variable isn't defined; not otherwise. Precede
> it with On Error Resume Next -- then it doesn't matter.
[quoted text clipped - 14 lines]
> >> >
> >> > Thanks...