Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Help with macro converting list elements.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MKruer@gmail.com - 27 Jul 2007 16:23 GMT
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
[list][*]One
[*]Two
[*]Three[/list]

List Numbers
[list=1][*]One
[*]Two
[*]Three[/list]

List Letters
[list=a][*]One
[*]Two
[*]Three[/list]

Complex List
[list=1][*]One
[list=a][*]One
[*]Two
[list=1][*]One
[*]Two
[*]Three[/list]
[*]Three[/list]
[*]Two
[*]Three[/list]
[/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 "[list=1]"
Next
End Sub
[/VB code]
Russ - 29 Jul 2007 19:57 GMT
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
[quoted text clipped - 94 lines]
> End Sub
> [/VB code]

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 29 Jul 2007 20:08 GMT
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.
[quoted text clipped - 134 lines]
>> End Sub
>> [/VB code]

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 29 Jul 2007 20:21 GMT
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.
[quoted text clipped - 163 lines]
>>> End Sub
>>> [/VB code]

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.