Udostępnij za pośrednictwem


Using VBA to turn numbering into normal text in Word documents

As you probably know by now, I spend a lot of my time writing up proposed edits to the file format standard IS 29500 in Word. The end result of what I'm doing always ends up being sent to my ISO colleagues on WG4 in another document. I'll be editing something that looks like this:

When I copy and paste this into a new document to send to my colleagues, it unfortunately looks like this:

This is because Word faithfully maintained my numbering settings, and the proceeding numbered sections are all gone. You may come across similar headaches when you want to edit a large document where the section numbering must remain constant, and you don't want to mess with it by mistake.

Well, I wrote some code to turn the numbered sections in my document into normal paragraph text. It works on numbered lists as well as numbered sections.

I'll not go into great detail about how it works, but basically it's a two stage process - I first go through the document adding the numbering text to each numbered paragraph, then I go through the document again deleting the numbering from the paragraphs. Why not do it all at once? Well, that's what I tried first, actually. You probably realised faster than I did that I ended up with a document which contained a large number of "1"s. Why not go through the paragraphs in reverse order? A fine question - I'm afraid to say that using the Word object model to index into the Paragraphs collection is very slow indeed - my guess is that when Word is asked for Paragraphs(186543) it needs to go through the prior ones first to find it. Using For Each is much faster than indexing, and I guess I can see why.

As you'll know if you run this code on a large document, it's as slow as the proverbial anyway. For thousands of pages, you may wish to prepare a cup of tea.

The code is below - as ever, if you have any fixes or changes, I'd love to hear them. Please bear in mind that I wrote this for my own purposes and it's not really been tested at all - I noticed today, for example, that it seems to remove indentation on paragraphs which doesn't matter to me but might matter to you. Please don't run this on your documents without keeping a copy of them first!

 

Private Sub RemoveNumberingOnAllSections()
Dim i As Paragraph, lastsaved As Date
' Convert all important sections to straight text (no numbering)
For Each i In ActiveDocument.Paragraphs
If IsNumberedPara(i.Range) Then
ConvertNumberedToTextNoDelete i.Range
ActiveDocument.UndoClear
End If
Next

    For Each i In ActiveDocument.Paragraphs
If IsNumberedPara(i.Range) Then
i.Range.ListFormat.RemoveNumbers
ActiveDocument.UndoClear
End If
Next
End Sub

Private Sub ConvertNumberedToTextNoDelete(rOf As Range)
If InStr(rOf.ListFormat.ListString, ".") > 0 Then
If Left(rOf.Text, Len(rOf.ListFormat.ListString)) <> rOf.ListFormat.ListString Then
' Haven't done this one already
rOf.InsertBefore rOf.ListFormat.ListString + " "
End If
End If
End Sub

Private Function IsNumberedPara(rOf As Range) As Boolean
If rOf Is Nothing Then ' if the document doesn't have numbered sections, this will cause changes to be enumerated in the whole thing
IsNumberedPara = True
ElseIf rOf.ListFormat.ListString <> "" Then
If Asc(rOf.ListFormat.ListString) <> 63 Then
IsNumberedPara = True
End If
End If
End Function

Comments

  • Anonymous
    December 28, 2010
    Selection.Range.ListFormat.ConvertNumbersToText
  • Anonymous
    January 04, 2011
    Superb.
  • Anonymous
    December 22, 2011
    Here is a revision to your code that steps through the paragraphs backwards and converts the number to text.   For counter = ActiveDocument.Paragraphs.Count To 1 Step -1       If Len(Paragraphs(counter).Range.ListFormat.ListString) > 0 Then           Paragraphs(counter).Range.ListFormat.ConvertNumbersToText       End If    Next counter