Log in

View Full Version : Solved: AutoNum and ListNum in VBA



marcelma
10-09-2010, 09:22 AM
Hello,

does anyone know a way to read out autoNum and listNum fields?
I have a large file in which the paragraphs are numbered with autoNum fields, but I need to have them numbered in plain text.

thanks a lot in advance,
Marcel

Paul_Hossler
10-09-2010, 12:38 PM
Control-A to select the entire docuent, and Control-Shift-F9 to unlink the fields to their values

Paul

marcelma
10-09-2010, 12:53 PM
Geeee - is that coool!!! Thanks a lot Paul!

Why couldn't I find that? I have been looking through VBA helpfiles for hours!

macropod
10-09-2010, 11:16 PM
Hi Marcel,

In vba:
ActiveDocument.Fields.Unlink

However, and this is important to note, both techniques convert all other fields in the body of the document (eg hyperlinks, cross-references) to plain text also. This may or may not be desirable.

marcelma
10-10-2010, 01:39 AM
In this special case it made no difference and was a fast and welcome solution, but since I am working on a more general solution, it would indeed be good to know how this could be accomplished by VBA on an individual basis. The code could then check the context and determine whether a certain field should indeed be unlinked or not. Yesterday I tried:

Selection.Fields.Unlink

but while this works fine on a 1st occurence of AutoNum (that is, with the number "1"), the next AutoNum fields starts counting from "1" again, although I would need it to still be "2".

Any idea?

thanks a lot in advance,
Marcel

macropod
10-10-2010, 02:04 AM
Hi Marcel,

As you've found, if you unlink and autonum or listnum field numbered '1', '2' becomes '1'. Here's a macro to unlink all of them in reverse order, thus preserving the original numbering:

Sub UnLinkNumFields()
Dim i As Integer
With ActiveDocument
For i = .Fields.Count To 1 Step -1
With .Fields(i)
If .Type = wdFieldAutoNum Then .Unlink
If .Type = wdFieldListNum Then .Unlink
End With
Next
End With
End Sub

marcelma
10-10-2010, 02:35 PM
that's a neat little program, which comes in very handy - thanks a lot :-)

Marcel

macropod
10-10-2010, 03:01 PM
Hi Marcel,

I was re-thinking this last night and concluded it would be better to code as:

Sub UnLinkNumFields()
Dim i As Integer
With ActiveDocument
For i = .Fields.Count To 1 Step -1
With .Fields(i)
If .Type = wdFieldAutoNum Or .Type = wdFieldListNum Then .Unlink
End With
Next
End With
End Sub
With the two separate if tests, the second one causes an error if the first test unlinked the field. This way, the field exists while both conditions are being tested.

fumei
10-12-2010, 10:29 AM
Smart.