# word macro to format a bullet list



## Norto23 (May 18, 2012)

Hi all, I'm wanting a macro to find instances of bullet lists, and replace it with a html tagged list. Please see below for an example:

...
text,text,text
My bullet list:


List point one
List point two


Some more text here.
...

Replaced with
...
text,text,text
My bullet list:
<ul>
<li>List point one</li>
<li>List point two</li>
</ul>
Some more text here.
...

Does anyone know of a way to do this? I've tried looking for the bullet characters; doesn't work as it's formatting. Tried looking for lines with style "List bullet" and any other lists i can find; doesn't work, I don't know why.


----------



## macropod (Apr 11, 2008)

Taking the code in http://www.techsupportforum.com/forums/f57/insert-html-tags-into-word-document-646040.html as a starting point, you could add the following lines before the last 'End With' line.

```
.ClearFormatting
    .Style = "List Bullet"
    .Text = "[!^13]{1,}"
    With .Replacement
      .ClearFormatting
      .Text = "<li>^&</li>"
    End With
    .Execute Replace:=wdReplaceAll
    .Text = ""
    With .Replacement
      .Text = "<ul>^&</ul>"
    End With
    .Execute Replace:=wdReplaceAll
```


----------



## Norto23 (May 18, 2012)

Thanks for the reply. I couldn't get this working, I tried things similar to this earlier today. Tried all the different stylings in my system!!! (Maybe beacause I'm on a mac??)

Anyways I found another script to iterate through each line of the document and add to a counter. Do you know if there would be a way to append to the found paragraph <li> tags on either end... something like below???


 Sub FindBullet()
Dim oPara As Word.Paragraph
Dim count As Integer

count = 0
Selection.WholeStory
With Selection
For Each oPara In .Paragraphs
If oPara.Range.ListFormat.ListType = _
WdListType.wdListBullet Then

count = count + 1

# from here down it gets shaky!!!

With ActiveDocument.Range.Find
.Text = #How do i convert the oPara to a string here?!?
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

.ClearFormatting
With .replacement
.ClearFormatting
.Text = # how do i specify this is where i want the "<li>" & oPara & "</li>"
End With
.Execute Replace:=wdReplaceAll

End If
Next
End With
'Gives you the count of bullets in a document
MsgBox count & " replacements"
End Sub


----------



## macropod (Apr 11, 2008)

The code I posted looks for a "List Bullet" Style, as per your original post. Are you sure that's the correct Style name?


----------



## Norto23 (May 18, 2012)

Yeah I can see that style in the formatting part, Word for Mac 2011 just isn't picking it up. When I run the above macro (minus the find/replace code) I can pick it up.

Do you know how I can use the find and replace as per above??


----------



## macropod (Apr 11, 2008)

Try the following. It does all the tag processing from the previous thread as well, in a slightly more sophisticated manner.

```
Sub AddTags()
Dim oPara As Paragraph
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .ClearFormatting
    .Font.Bold = True
    With .Replacement
      .ClearFormatting
      .Text = "<b>^&</b>^p"
      .Font.Bold = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Text = "^p</b>"
    .Replacement.Text = "</b>"
    .Execute Replace:=wdReplaceAll
    .Font.Italic = True
    With .Replacement
      .ClearFormatting
      .Text = "<i>^&</i>^p"
      .Font.Italic = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Text = "^p</i>"
    .Replacement.Text = "</i>"
    .Execute Replace:=wdReplaceAll
    .Font.Underline = True
    With .Replacement
      .ClearFormatting
      .Text = "<u>^&</u>^p"
      .Font.Underline = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Text = "^p</u>"
    .Replacement.Text = "</u>"
    .Execute Replace:=wdReplaceAll
    .Replacement.ClearFormatting
    .Wrap = wdFindStop
    .Style = "List Bullet"
    .Text = ""
    .Replacement.Text = "<ul>^&"
    .Execute Replace:=wdReplaceAll
    .Execute
  End With
  Do While .Find.Found
    For Each oPara In .Duplicate.Paragraphs
      With oPara.Range
        If InStr(.Text, "<ul>") = 1 Then .Start = .Start + 4
        .InsertBefore "<li>"
        .Characters.Last.InsertBefore "</li>"
      End With
    Next
    .Duplicate.Collapse wdCollapseEnd
    .Find.Execute
  Loop
  .Start = ActiveDocument.Range.Start
  With .Find
    .ClearFormatting
    .Style = "List Bullet"
    .Text = ""
    .Replacement.Text = "^&</ul>^p"
    .Execute Replace:=wdReplaceAll
    .Text = "^p</ul>"
    .Replacement.Text = "</ul>"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
```


----------

