• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 75
  • Last Modified:

Using VBA in Word, convert numbered list (A, B, C, ...) to non-list (A tab, B tab, C tab, etc.)

I have over 100 instances of MS Word's numbered lists (A, B, C, D, E) scattered throughout a file.  I have written VBA code that converts this to straight text and inserts the appropriate letter and a tab (e.g., A + tab) at the beginning of each line. However, I have to manually select each list. What VBA code will automatically select each list and execute the code that converts it?

Here is my VBA code that converts a selected list.  By the way, every list is A, B, C, D, E.

'Convert the selected list to a non-list, and put A+tab, B+tab, or whatever, at the beginning of each paragraph
    Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
    Selection.MoveLeft unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="A" & vbTab
    Selection.MoveDown unit:=wdParagraph, Count:=1
    Selection.TypeText Text:="B" & vbTab
    Selection.MoveDown unit:=wdParagraph, Count:=1
    Selection.TypeText Text:="C" & vbTab
    Selection.MoveDown unit:=wdParagraph, Count:=1
    Selection.TypeText Text:="D" & vbTab
    Selection.MoveDown unit:=wdParagraph, Count:=1
    Selection.TypeText Text:="E" & vbTab

Open in new window

0
gordonwwaugh
Asked:
gordonwwaugh
  • 7
  • 5
1 Solution
 
FlysterCommented:
This should loop through your lists:
Sub ListLoop()
Dim i, numLists As Integer

  numLists = ActiveDocument.ListParagraphs.Count

    For i = 1 To numLists
     'Convert the selected list to a non-list, and put A+tab, B+tab, or whatever, at the beginning of each paragraph
      Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
      Selection.MoveLeft unit:=wdCharacter, Count:=1
      Selection.TypeText Text:="A" & vbTab
      Selection.MoveDown unit:=wdParagraph, Count:=1
      Selection.TypeText Text:="B" & vbTab
      Selection.MoveDown unit:=wdParagraph, Count:=1
      Selection.TypeText Text:="C" & vbTab
      Selection.MoveDown unit:=wdParagraph, Count:=1
      Selection.TypeText Text:="D" & vbTab
      Selection.MoveDown unit:=wdParagraph, Count:=1
      Selection.TypeText Text:="E" & vbTab
    Next

End Sub

Open in new window

Flyster
0
 
gordonwwaughAuthor Commented:
Flyster:

Your code does count the number of instances of numbered lists. But it does not move to the next numbered list. Because of that, I'm not sure that counting the number of numbered lists is useful. Perhaps the loop can simply execute (with a Find command in the code) until it's at the end of the file. So, I think the key, now is figuring out what the Find command should look like.
0
 
gordonwwaughAuthor Commented:
Okay. I got everything to work except one line of code.

My Word document has several 5-item lists. I want to replace the first item with "A [tab] the original text", the second item with "B [tab] the original text", etc. The code is supposed to loop through the document 5 times. The first time through, it revises the 5th item on the list, the second time through, it revises the 4th item on the list, etc.

The following line of code is supposed to go back to the top of the document when the bottom of the document is reached.
  Selection.HomeKey unit:=wdStory
 It works the very first time. That is, if I am in the middle of the document when the macro starts, then it goes to the top of the document. For some reason, it doesn't go back to the top of the file again. Why not?

Here is the complete code:

    Dim strActualLetter As String
    Dim strTargetLetter As String
    Dim intOptionNumber As Integer
    Dim fEOD As Boolean
    Dim intParagraphNumber As Integer
    
    'Go to the top of the file, go through every paragraph, and remove formatting for Option E.
    'The second time through, remove formatting for Option D, etc.
    
    For intOptionNumber = 5 To 1 Step -1
        ActiveDocument.Range.MoveStart
        Selection.HomeKey unit:=wdStory 'Go to the top of the document. This works only the first time through.
        strTargetLetter = Mid("ABCDE", intOptionNumber, 1)
        fEOF = False
        intParagraphNumber = 1
        
        MsgBox "strTargetLetter = " & strTargetLetter
        
        Do Until fEOD
            If Selection.Range.ListFormat.ListType > 0 Then
                strActualLetter = Mid("ABCDE", Selection.Range.ListFormat.ListValue, 1)

                If strActualLetter = strTargetLetter Then
                    Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
                    Selection.TypeText Text:=strActualLetter & vbTab
                End If
                
            End If
            
            Selection.MoveDown unit:=wdParagraph, Count:=1
            intParagraphNumber = intParagraphNumber + 1
            
            If Selection.Bookmarks.Exists("\EndOfDoc") = True Then
                fEOD = True
            End If
        Loop
    Next

Open in new window

0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
DrTribosCommented:
I'd suggest going through the document backwards... each time you delete a list the number of list items will be wrong...

For i = ActiveDocument.ListParagraphs.Count to 1 Step -1
' Process your list here

Next i

Open in new window

0
 
DrTribosCommented:
Ooops sorry I see that you're already on top of the Step -1 concept...
0
 
DrTribosCommented:
Ok... I just tested your code and it does not seem to work on my PC... my test doc has 10 paras and a list of 3 (A, B, C) which was created by using the "Number Format Button"...

Do you know if all the lists in your document are created in the same way?
0
 
gordonwwaughAuthor Commented:
Sorry, I don't recall how my lists were created. I am using Office 2013, by the way.
0
 
DrTribosCommented:
Try this....
Sub ChangeMyList()

Dim oPara As Paragraph
Dim oDoc As Document
Dim iCount As Integer
Dim strListChar As String

Set oDoc = ActiveDocument

For iCount = oDoc.Paragraphs.Count To 1 Step -1

    If oDoc.Paragraphs(iCount).Range.ListFormat.ListString <> "" Then
        Set oPara = oDoc.Paragraphs(iCount)
        strListChar = oPara.Range.ListFormat.ListString & vbTab
        Debug.Print strListChar
        oPara.Range.ListFormat.RemoveNumbers
        oPara.Range.InsertBefore strListChar
        
    End If

Next iCount
End Sub

Open in new window

0
 
DrTribosCommented:
Word or warning... the macro is not specific to Alphabetical lists...

A bullet list might end up looking like this:
q    bullet

(where, q is the plain text version of the bullet but could be some other character)

It will also work on numbers...

There is more than 1 way to add a list to a Word document - it might need to be tweaked to work with other list 'types'
0
 
gordonwwaughAuthor Commented:
Excellent. I tried it on numbers, a b c d, and A B C D. It worked on all of them. For standard bullets, it works too; it changed the bullets to an undisplayed character (on my computer, it appears as a question mark with a box around it).
0
 
DrTribosCommented:
Thanks... something looks funny with the dates on this question - mt PC suggests it was asked a looooong time ago....
Using VBA in Word, convert numbered list (A, B, C, ...) to non-list (A tab, B tab, C tab, etc.)
Question by:  gordonwwaugh On 2015-02-09 03:17 PM  Views 9

Does that seem right to you? If not I might bring this to the Mods attention...

Cheers
0
 
gordonwwaughAuthor Commented:
Yes, it was asked a very long time ago.
0
 
DrTribosCommented:
Ah...
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now