none
VBA Word bullet points falling over on second loop? Get me a bullet please... RRS feed

  • Question

  • Hi All,

    Right so we get data from our sql box from there we format the data coming out using a template file? This works in all cases ie: Bold, Center, Line breaks page breaks blah blah...

    We have a problem were the first loop works and the second loop leaves the LI and doesnt add the bullet points...

    Can you help???

    So the data looks something like this?
    <ul>
    <li>This is 1</li>
    <li>This is 2</li>
    <li>This is 3</li>
    </ul>
    Normal sentence
    <ul><li>This is 1</li></ul>
    <ul><li>This is 2</li></ul>
    <ul><li>This is 3</li></ul>


    Dim StartWord As String, EndWord As String, StartItem As String, EndItem As String

    ' Set Markup tags to search for, i.e. <ul></ul>, <li></li>
    StartList = "?ul^0062"
    EndList = "?^0047ul?"

    StartItem = "?li^0062"
    EndItem = "?^0047li?"

    ' Clear the existing Find criteria from the Selection
    Selection.Find.ClearFormatting

    ' Set the Find criteria
    With Selection.Find
    .Text = StartList & "*" & EndList
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    End With

    ' Loop through results of find, modifying the formatting
    While Selection.Find.Execute
    Selection.Range.Select
    Selection.Text = Replace(Replace(Selection.Text, "<ul>", ""), "</ul>", "")
    Selection.Range.ListFormat.ApplyBulletDefault (wdWord9ListBehavior)

    Dim r As Range
    Dim orig As Range
    Set r = Selection.Range
    Set orig = Selection.Range
    ' Set the Find criteria for the List Items
    With r.Find
    .ClearFormatting
    .Text = StartItem & "*" & EndItem
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    ' Loop through results of find, modifying the formatting
    End With
    While r.Find.Execute And r.InRange(orig)
    r.Select
    Selection.TypeText Replace(Replace(r, "<li>", ""), "</li>", "")
    Selection.TypeParagraph
    Wend

    Selection.Range.ListFormat.ApplyBulletDefault (wdWord9ListBehavior)
    Wend

    Wednesday, July 27, 2011 4:48 PM

All replies

  • Try using the command: Selection.ClearFormatting before "With r.Find" and take out the ".ClearFormatting" line.

    I think you are clearing the formatting every time the loop calls "r."

    Wednesday, March 14, 2012 9:29 PM