1
votes

How do I create a macro in VBA (Word) that will run through:

<ul><li>this is just a test</li><li>woop</li><li>test</li></ul>

and generate a

  • this is just a test
  • woop
  • test

Using Word 2010.

Reason for this is the html comes in via mailmerge from a 3rd party system.

This HAS to be done through a macro for my endusers! :)

More Details: I will have to create something as the following that searches for the Italic html tag and inserts Font.Italic instead.

With ActiveDocument.Range.Find
    .ClearFormatting
    .MatchCase = False
    .MatchWildcards = True
    .Text = "[<][iI][>]*[<]/[iI][>]"
    .Replacement.Font.Italic = True
    .Execute Replace:=wdReplaceAll
End With

So my question is, how do I, like this, run through the document and finds the <ul> instance and loops through each of the <li> tags and inserts the text in the tag as a bulleted list...

1

1 Answers

1
votes

Here is a simple way to achieve what you want.

LOGIC

  1. Create an IE instance
  2. Set the .InnerHTML to your html string
  3. Copy the contents from IE to word

In Short, let IE do the dirty work.

CODE

Option Explicit

Sub Sample()
    Dim ie As Object
    Dim strHtml As String

    strHtml = "<ul><li>this is just a test</li><li>woop</li><li>test</li></ul>"

    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True

    ie.Navigate "about:blank"

    ie.Document.body.InnerHTML = strHtml

    Do While ie.readystate <> 4: DoEvents: Loop

    ie.Document.body.createtextrange.execCommand "Copy"

    Selection.Paste
End Sub

SCREENSHOT

enter image description here