Help with macro converting list elements.

M

MKruer

I am working on a macro that will convert MS Word markup into a
BBCode. I have it partly working for list elements, but I cant seem to
figure out the rest. The problem is when it comes to multiple levels
of list elements, (see complex list below) Every time the bullets or
numbering changes or is indented, a new list element should be
generated. I am posting the MS word, the expected BBcode and the VB
code i am using. Any help will be appreciated.

Thank you

-Matt-

[MS word]
List Bullets
· One
· Two
· Three

List Numbers
1. One
2. Two
3. Three

List Letters
A. One
B. Two
C. Three

Complex List
1. One
a. One
b. Two
i. One
ii. Two
iii. Three
c. Three
2. Two
3. Three
[/MS word]

[expected BBCode]
List Bullets
  • One
  • Two
  • Three

List Numbers
  1. One
  2. Two
  3. Three

List Letters
  • One
  • Two
  • Three

Complex List
  1. One
    • One
    • Two
      1. One
      2. Two
      3. Three
    • Three
  2. Two
  3. Three
[/expected BBcode]

[VB code]
Sub ConvertLists()
Dim lCnt As Long
Dim lLst As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
lLst = rDcm.ListParagraphs.Count
For lCnt = lLst To 1 Step -1
With rDcm.ListParagraphs(lCnt).Range
.Select
If .Characters.Last.Previous <> "]" Then
.Characters.Last.InsertBefore "[/list]"
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End If
End With
While
Selection.Range.Paragraphs(1).Previous(1).Range.ListParagraphs.Count
<> 0
lCnt = lCnt - 1
With rDcm.ListParagraphs(lCnt).Range
.Select
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End With
Wend
Selection.Range.InsertBefore "
  1. "
    Next
    End Sub
    [/VB code]
 
R

Russ

Matt,
Here is more info.
You could iterate through the lists collection and listlevels collection and
parse the liststring. Also the number of tabs in a paragraph might give you
a clue of listlevel. Of course, all this relies on non-plain-text lists.
-----------------------------------------
<http://www.word.mvps.org/FAQs/Numbering/ListString.htm>
-----------------------------------------
Fields:
wdFieldType = wdAutoNum, wdAutoNumLegal, wdAutoNumOutline,
-----------------------------------------
Word VBA Help Examples:

Set myDoc = Documents("MyLetter.doc")
i = myDoc.Lists.Count
For each li in myDoc.Lists
Msgbox "List " & i & " has " & li.CountNumberedItems & " items."
i = i - 1
Next li

-----------------------------------------
This example creates an alternating number style for the third
outline-numbered list template.
Set myTemp = ListGalleries(wdOutlineNumberGallery).ListTemplates(3)
For i = 1 to 9
If i Mod 2 = 0 Then
myTemp.ListLevels(i).NumberStyle = wdListNumberStyleUppercaseRoman
Else
myTemp.ListLevels(i).NumberStyle = wdListNumberStyleLowercaseRoman
End If
Next i
This example changes the number style to uppercase letters for every
outline-numbered list in the active document.
For Each lt In ActiveDocument.ListTemplates
For Each ll In lt.listlevels
ll.NumberStyle = wdListNumberStyleUppercaseLetter
Next ll
Next lt
-----------------------------------------



I am working on a macro that will convert MS Word markup into a
BBCode. I have it partly working for list elements, but I cant seem to
figure out the rest. The problem is when it comes to multiple levels
of list elements, (see complex list below) Every time the bullets or
numbering changes or is indented, a new list element should be
generated. I am posting the MS word, the expected BBcode and the VB
code i am using. Any help will be appreciated.

Thank you

-Matt-

[MS word]
List Bullets
· One
· Two
· Three

List Numbers
1. One
2. Two
3. Three

List Letters
A. One
B. Two
C. Three

Complex List
1. One
a. One
b. Two
i. One
ii. Two
iii. Three
c. Three
2. Two
3. Three
[/MS word]

[expected BBCode]
List Bullets
  • One
  • Two
  • Three

List Numbers
  1. One
  2. Two
  3. Three

List Letters
  • One
  • Two
  • Three

Complex List
  1. One
    • One
    • Two
      1. One
      2. Two
      3. Three
    • Three
  2. Two
  3. Three
[/expected BBcode]

[VB code]
Sub ConvertLists()
Dim lCnt As Long
Dim lLst As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
lLst = rDcm.ListParagraphs.Count
For lCnt = lLst To 1 Step -1
With rDcm.ListParagraphs(lCnt).Range
.Select
If .Characters.Last.Previous <> "]" Then
.Characters.Last.InsertBefore "[/list]"
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End If
End With
While
Selection.Range.Paragraphs(1).Previous(1).Range.ListParagraphs.Count
<> 0
lCnt = lCnt - 1
With rDcm.ListParagraphs(lCnt).Range
.Select
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End With
Wend
Selection.Range.InsertBefore "
  1. "
    Next
    End Sub
    [/VB code]
 
R

Russ

Matt,
In case you want to change the lists to plain text.
Shauna mentioned in another message to convert a copy of the document.(i.e.
and post the copy to your bulletin board.)

Word VBA Help:
ConvertNumbersToText Method

Changes the list numbers and LISTNUM fields in the specified Document, List,
or ListFormat object to text.
Syntax
expression.ConvertNumbersToText(NumberType)
expression Required. An expression that returns a Document, List, or
ListFormat object.
NumberType Optional Variant. The type of number to be converted. Can be
one of the following WdNumberType constants: wdNumberParagraph,
wdNumberListNum, or wdNumberAllNumbers. The default value is
wdNumberAllNumbers.
Remarks
There are two types of numbers: preset numbers (wdNumberParagraph), which
you can add to paragraphs by selecting a template in the Bullets and
Numbering dialog box; and LISTNUM fields (wdNumberListNum), which allow you
to add more than one number per paragraph.
The ConvertNumbersToText method is useful if you want to work with a
document in another application and that application doesn't recognize list
formatting or LISTNUM fields.
Note After you've converted list numbers to text, you can no longer
manipulate them in a list.

Matt,
Here is more info.
You could iterate through the lists collection and listlevels collection and
parse the liststring. Also the number of tabs in a paragraph might give you
a clue of listlevel. Of course, all this relies on non-plain-text lists.
-----------------------------------------
<http://www.word.mvps.org/FAQs/Numbering/ListString.htm>
-----------------------------------------
Fields:
wdFieldType = wdAutoNum, wdAutoNumLegal, wdAutoNumOutline,
-----------------------------------------
Word VBA Help Examples:

Set myDoc = Documents("MyLetter.doc")
i = myDoc.Lists.Count
For each li in myDoc.Lists
Msgbox "List " & i & " has " & li.CountNumberedItems & " items."
i = i - 1
Next li

-----------------------------------------
This example creates an alternating number style for the third
outline-numbered list template.
Set myTemp = ListGalleries(wdOutlineNumberGallery).ListTemplates(3)
For i = 1 to 9
If i Mod 2 = 0 Then
myTemp.ListLevels(i).NumberStyle = wdListNumberStyleUppercaseRoman
Else
myTemp.ListLevels(i).NumberStyle = wdListNumberStyleLowercaseRoman
End If
Next i
This example changes the number style to uppercase letters for every
outline-numbered list in the active document.
For Each lt In ActiveDocument.ListTemplates
For Each ll In lt.listlevels
ll.NumberStyle = wdListNumberStyleUppercaseLetter
Next ll
Next lt
-----------------------------------------



I am working on a macro that will convert MS Word markup into a
BBCode. I have it partly working for list elements, but I cant seem to
figure out the rest. The problem is when it comes to multiple levels
of list elements, (see complex list below) Every time the bullets or
numbering changes or is indented, a new list element should be
generated. I am posting the MS word, the expected BBcode and the VB
code i am using. Any help will be appreciated.

Thank you

-Matt-

[MS word]
List Bullets
· One
· Two
· Three

List Numbers
1. One
2. Two
3. Three

List Letters
A. One
B. Two
C. Three

Complex List
1. One
a. One
b. Two
i. One
ii. Two
iii. Three
c. Three
2. Two
3. Three
[/MS word]

[expected BBCode]
List Bullets
  • One
  • Two
  • Three

List Numbers
  1. One
  2. Two
  3. Three

List Letters
  • One
  • Two
  • Three

Complex List
  1. One
    • One
    • Two
      1. One
      2. Two
      3. Three
    • Three
  2. Two
  3. Three
[/expected BBcode]

[VB code]
Sub ConvertLists()
Dim lCnt As Long
Dim lLst As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
lLst = rDcm.ListParagraphs.Count
For lCnt = lLst To 1 Step -1
With rDcm.ListParagraphs(lCnt).Range
.Select
If .Characters.Last.Previous <> "]" Then
.Characters.Last.InsertBefore "[/list]"
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End If
End With
While
Selection.Range.Paragraphs(1).Previous(1).Range.ListParagraphs.Count
<> 0
lCnt = lCnt - 1
With rDcm.ListParagraphs(lCnt).Range
.Select
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End With
Wend
Selection.Range.InsertBefore "
  1. "
    Next
    End Sub
    [/VB code]
 
R

Russ

Matt,
Another thing that you might be able to parse is the LISTNUM field contents.

But, nevertheless, type listnum into Word VBA help for more methods and
properties pertaining to list.
Matt,
In case you want to change the lists to plain text.
Shauna mentioned in another message to convert a copy of the document.(i.e.
and post the copy to your bulletin board.)

Word VBA Help:
ConvertNumbersToText Method

Changes the list numbers and LISTNUM fields in the specified Document, List,
or ListFormat object to text.
Syntax
expression.ConvertNumbersToText(NumberType)
expression Required. An expression that returns a Document, List, or
ListFormat object.
NumberType Optional Variant. The type of number to be converted. Can be
one of the following WdNumberType constants: wdNumberParagraph,
wdNumberListNum, or wdNumberAllNumbers. The default value is
wdNumberAllNumbers.
Remarks
There are two types of numbers: preset numbers (wdNumberParagraph), which
you can add to paragraphs by selecting a template in the Bullets and
Numbering dialog box; and LISTNUM fields (wdNumberListNum), which allow you
to add more than one number per paragraph.
The ConvertNumbersToText method is useful if you want to work with a
document in another application and that application doesn't recognize list
formatting or LISTNUM fields.
Note After you've converted list numbers to text, you can no longer
manipulate them in a list.

Matt,
Here is more info.
You could iterate through the lists collection and listlevels collection and
parse the liststring. Also the number of tabs in a paragraph might give you
a clue of listlevel. Of course, all this relies on non-plain-text lists.
-----------------------------------------
<http://www.word.mvps.org/FAQs/Numbering/ListString.htm>
-----------------------------------------
Fields:
wdFieldType = wdAutoNum, wdAutoNumLegal, wdAutoNumOutline,
-----------------------------------------
Word VBA Help Examples:

Set myDoc = Documents("MyLetter.doc")
i = myDoc.Lists.Count
For each li in myDoc.Lists
Msgbox "List " & i & " has " & li.CountNumberedItems & " items."
i = i - 1
Next li

-----------------------------------------
This example creates an alternating number style for the third
outline-numbered list template.
Set myTemp = ListGalleries(wdOutlineNumberGallery).ListTemplates(3)
For i = 1 to 9
If i Mod 2 = 0 Then
myTemp.ListLevels(i).NumberStyle = wdListNumberStyleUppercaseRoman
Else
myTemp.ListLevels(i).NumberStyle = wdListNumberStyleLowercaseRoman
End If
Next i
This example changes the number style to uppercase letters for every
outline-numbered list in the active document.
For Each lt In ActiveDocument.ListTemplates
For Each ll In lt.listlevels
ll.NumberStyle = wdListNumberStyleUppercaseLetter
Next ll
Next lt
-----------------------------------------



I am working on a macro that will convert MS Word markup into a
BBCode. I have it partly working for list elements, but I cant seem to
figure out the rest. The problem is when it comes to multiple levels
of list elements, (see complex list below) Every time the bullets or
numbering changes or is indented, a new list element should be
generated. I am posting the MS word, the expected BBcode and the VB
code i am using. Any help will be appreciated.

Thank you

-Matt-

[MS word]
List Bullets
· One
· Two
· Three

List Numbers
1. One
2. Two
3. Three

List Letters
A. One
B. Two
C. Three

Complex List
1. One
a. One
b. Two
i. One
ii. Two
iii. Three
c. Three
2. Two
3. Three
[/MS word]

[expected BBCode]
List Bullets
  • One
  • Two
  • Three

List Numbers
  1. One
  2. Two
  3. Three

List Letters
  • One
  • Two
  • Three

Complex List
  1. One
    • One
    • Two
      1. One
      2. Two
      3. Three
    • Three
  2. Two
  3. Three
[/expected BBcode]

[VB code]
Sub ConvertLists()
Dim lCnt As Long
Dim lLst As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
lLst = rDcm.ListParagraphs.Count
For lCnt = lLst To 1 Step -1
With rDcm.ListParagraphs(lCnt).Range
.Select
If .Characters.Last.Previous <> "]" Then
.Characters.Last.InsertBefore "[/list]"
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End If
End With
While
Selection.Range.Paragraphs(1).Previous(1).Range.ListParagraphs.Count
<> 0
lCnt = lCnt - 1
With rDcm.ListParagraphs(lCnt).Range
.Select
.InsertBefore "[*]"
.ListFormat.RemoveNumbers
End With
Wend
Selection.Range.InsertBefore "
  1. "
    Next
    End Sub
    [/VB code]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top