Unlink all fields except Formula field

S

singeredel

I am trying to find a way to write code to unlink all the fields in a
document except a field that has an arithmetic formula in it. The particular
formula in this regard happens to appear in a table cell. The following code
unlinks ALL the fields in the document, including the field in the table cell
that has a formula in it. What am I doing wrong? Thanks for any help...

Dim af As Field
With ActiveDocument
For Each af In .Fields
If af.Type <> wdFieldFormula Then
af.Unlink
End If
Next af
End With
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
I am trying to find a way to write code to unlink all the fields in a
document except a field that has an arithmetic formula in it. The
particular formula in this regard happens to appear in a table cell.
The following code unlinks ALL the fields in the document, including
the field in the table cell that has a formula in it. What am I doing
wrong? Thanks for any help...

Dim af As Field
With ActiveDocument
For Each af In .Fields
If af.Type <> wdFieldFormula Then
af.Unlink
End If
Next af
End With

Try:

If af.Type <> wdFieldExpression Then

instead.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

singeredel

Thank you so much! That worked, although I don't understand the various
definitions. Is there some place I can go to find out what these different
members of the wdFieldType are? I tried plugging it into the VBA search, but
came up empty.
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
Thank you so much! That worked, although I don't understand the
various definitions. Is there some place I can go to find out what
these different members of the wdFieldType are? I tried plugging it
into the VBA search, but came up empty.

From the VBE, do F2 to access the object browser. In the object list (The
narrow pane on the left), find wdFieldType. Click on it and you will get the
list of all the available types in the wider pane on the right. When you
select one, you will also get its numerical constant value (at the bottom)
which can be useful when you do late binding.

Unfortunately, there is no definition for each field type,

Sometimes, if you have a field in a document and are unsure as to its type,
select it and run this code:
Msgbox Selection.Field(1).Type
or, in the immeditate window
? Selection.Field(1).Type (Enter)
this will return the constant value. Then you need to find it in the object
browser to get its actual name.

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