Word 2 HTML VBA宏

时间:2015-11-05 02:55:35

标签: html vba ms-word ms-office word-vba

我有多个word文档,所有文档格式相同。现在我将所有这些作为内部帮助门户的HTML页面。我想创建一个宏,当运行时自动生成有效的HTML文件,我可以直接上传到Web服务器。我能够使用vba代码将所有内容作为HTML代码获取。但我被困在列表段落中。我收到了所有列表项的<Li></Li>个标记,但我如何获得一个<ol><ul>标记呢?

示例文档位于here.

以下是我的VBA代码。

    Sub ListParagraphs()
Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs

If p.Style = ActiveDocument.Styles("Title") Then
Debug.Print "<h2>" & p.Range.Text & "</h2>"
End If

If p.Style = ActiveDocument.Styles("Heading 1") Then
Debug.Print "<h3>" & p.Range.Text & "</h3>"
End If

If p.Style = ActiveDocument.Styles("Heading 2") Then
Debug.Print "<h4>" & p.Range.Text & "</h4>"
End If

If p.Style = ActiveDocument.Styles("Normal") Then
Debug.Print "<p>" & p.Range.Text & "<p>"
End If

If p.Style = ActiveDocument.Styles(wdStyleListParagraph) Then
    p.Range.Select
    Selection.EndKey Unit:=wdLine
    Debug.Print "<li>" & p.Range.Text & "</li>"
End If

Next p

End Sub

我得到的输出是:

        <h2>Main Title of page
    </h2>
    <p>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.
    <p>
    <h3>Sub topic heading – number 1
    </h3>
    <p>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks.
    <p>
    <li>Instruction number one. You can use these galleries to insert tables, headers, footers, lists.
    </li>
    <li>/
    </li>
    <li>Instruction number two. This is a small step.
    </li>
    <li>/
    </li>
    <li>More instructions go here. 
    </li>
    <li>/
    </li>
    <h3>Subtopic heading - number 2
    </h3>
    <p>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.
    <p>
    <h4>Sub sub-topic under number 2
    </h4>
    <p>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document.
    <p>
    <li>Remember the following points. 
    </li>
    <li>/
    </li>
    <li>More instructions.
    </li>
    <li>/
    </li>
    <h4>Second sub topic under number 2
    </h4>
    <li>Line one.
    </li>
    <li>/
    </li>
    <li>Line 2.
    </li>
    <li>/
    </li>
    <p>
    <p>
    <p>
    <p>
    <p>
    <p>

1 个答案:

答案 0 :(得分:0)

哦,是的!非常感谢enhzflep给我提示!下面的代码按预期工作。

Dim lastElement As String
Dim typeElement As String

lastElement = "none"
typeElement = "none"
For Each p In ActiveDocument.Paragraphs

If p.Style = ActiveDocument.Styles("Title") Then
    If lastElement = "list" Then
        stream.WriteLine "</" & typeElement & ">"
    End If

    stream.WriteLine "<h2>" & p.Range.Text & "</h2>"
    lastElement = "title"

End If

If p.Style = ActiveDocument.Styles("Heading 1") Then
    If lastElement = "list" Then
            stream.WriteLine "</" & typeElement & ">"
    End If

stream.WriteLine "<h3>" & p.Range.Text & "</h3>"
lastElement = "heading1"

End If

If p.Style = ActiveDocument.Styles("Heading 2") Then
    If lastElement = "list" Then
            stream.WriteLine "</" & typeElement & ">"
    End If

stream.WriteLine "<h4>" & p.Range.Text & "</h4>"
lastElement = "heading2"

End If

If p.Style = ActiveDocument.Styles("Normal") Then
    If lastElement = "list" Then
            stream.WriteLine "</" & typeElement & ">"
    End If

stream.WriteLine "<p>" & p.Range.Text & "</p>"
lastElement = "normal"

End If

If p.Style = ActiveDocument.Styles(wdStyleListParagraph) Then
    If p.Range.ListFormat.ListType = wdListSimpleNumbering Then
       typeElement = "ol"
       ElseIf p.Range.ListFormat.ListType = wdListBullet Then
       typeElement = "ul"
    End If

    If lastElement <> "list" Then
        stream.WriteLine "<" & typeElement & ">"
    End If
    stream.WriteLine "<li>" & p.Range.Text & "</li>"
    lastElement = "list"

End If

Next p