1

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

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

并生成一个

  • 这只是一个测试
  • 呜呜
  • 测试

使用 Word 2010。

原因是 html 通过来自 3rd 方系统的 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>标签并将标签中的文本作为项目符号列表插入...

4

1 回答 1

1

这是实现您想要的简单方法。

逻辑

  1. 创建一个 IE 实例
  2. 设置.InnerHTML为您的 html 字符串
  3. 将IE中的内容复制到word

简而言之,让 IE 做脏活。

代码

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

截屏

在此处输入图像描述

于 2012-09-21T16:03:17.780 回答