2015-11-05 94 views
0

我有多個word文檔,所有格式都相同。現在我將所有這些內容作爲HTML頁面提供給內部幫助門戶。我想創建一個宏,當運行時自動生成有效的HTML文件,我可以直接上傳到Web服務器。我能夠使用vba代碼將所有內容都作爲HTML代碼呈現出來。但我堅持列表段落。我得到的所有列表項的<Li></Li>標籤,但我怎麼能得到一個<ol><ul>標籤surronding他們?Word 2 HTML VBA宏

樣品文件可於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> 
+0

順便說一句我知道互聯網上的word2HTML轉換網站,也做了我對這個問題的研究。我只是想要一個vba解決方案。 –

+1

您應該記錄添加的最後一個元素。當創建一個'li'時,檢查這個變量。如果它不是'li''你知道'li'是它們中的第一個,你應該先添加一個'ol'或'ul'。同樣,當您創建其他元素類型時,再次檢查最後一個元素。如果最後一個元素是'li',那麼你已經完成了一個塊,並且需要關閉開頭'ul'或'ol' – enhzflep

回答

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