简体   繁体   English

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

[英]Convert HTML list in word to bulleted list

How do I create a macro in VBA (Word) that will run through: 如何在VBA(Word)中创建一个宏,该宏将通过以下方式运行:

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

and generate a 并产生一个

  • this is just a test 这只是一个测试
  • woop oop
  • test 测试

Using Word 2010. 使用Word 2010。

Reason for this is the html comes in via mailmerge from a 3rd party system. 原因是html是通过第三方系统的mailmerge传入的。

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. 更多详细信息:我将必须创建以下内容,以搜索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

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

Here is a simple way to achieve what you want. 这是实现您想要的一种简单方法。

LOGIC 逻辑

  1. Create an IE instance 创建一个IE实例
  2. Set the .InnerHTML to your html string .InnerHTML设置为您的html字符串
  3. Copy the contents from IE to word 将内容从IE复制到word

In Short, let IE do the dirty work. 简而言之,让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 屏幕截图

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM