将单词中的HTML列表转换为项目符号列表

时间:2012-09-21 12:17:14

标签: vba ms-word word-vba

如何在VBA(Word)中创建将运行的宏:

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

并生成

  • 这只是一个测试
  • woop
  • test

使用Word 2010。

原因是html来自第三方系统的mailmerge。

这是通过我的最终用户的宏来完成的! :)

更多详情: 我将不得不创建以下内容来搜索Italic html标记并插入Font.Italic。

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

所以我的问题是,我如何像这样运行文档并找到<ul>实例并循环遍历每个<li>标记,并将标记中的文本作为项目符号插入列表...

1 个答案:

答案 0 :(得分:1)

这是实现目标的简单方法。

<强> LOGIC

  1. 创建IE实例
  2. .InnerHTML设置为您的html字符串
  3. 将内容从IE复制到文字
  4. 总之,让IE做脏事。

    <强> 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