Delete a DocVariable using VBA

S

singeredel

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...
 
J

Jay Freedman

singeredel said:
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
 
S

singeredel

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...
 
S

singeredel

Thanks for your help. I had previously tried this statement and it gave me an
error.
 
J

Jezebel

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.
 
H

Helmut Weber

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/
 
S

singeredel

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
 

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