SolvedPrivate

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

Posted on 2015-02-08
13
41 Views
Last Modified: 2016-02-23
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
Comment
Question by:gordonwwaugh
  • 7
  • 5
13 Comments
 
LVL 22

Expert Comment

by:Flyster
Comment Utility
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
 

Author Comment

by:gordonwwaugh
Comment Utility
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
 

Author Comment

by:gordonwwaugh
Comment Utility
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
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
Ooops sorry I see that you're already on top of the Step -1 concept...
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:gordonwwaugh
Comment Utility
Sorry, I don't recall how my lists were created. I am using Office 2013, by the way.
0
 
LVL 14

Accepted Solution

by:
DrTribos earned 500 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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
 

Author Closing Comment

by:gordonwwaugh
Comment Utility
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
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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
 

Author Comment

by:gordonwwaugh
Comment Utility
Yes, it was asked a very long time ago.
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
Ah...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now