gordonwwaugh
asked on
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.
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
ASKER
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.
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.
ASKER
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:
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
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
Ooops sorry I see that you're already on top of the Step -1 concept...
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?
Do you know if all the lists in your document are created in the same way?
ASKER
Sorry, I don't recall how my lists were created. I am using Office 2013, by the way.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'
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'
ASKER
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).
Thanks... something looks funny with the dates on this question - mt PC suggests it was asked a looooong time ago....
Does that seem right to you? If not I might bring this to the Mods attention...
Cheers
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
ASKER
Yes, it was asked a very long time ago.
Ah...
Open in new window
Flyster