2015-11-05 5 views
0

У меня есть несколько документов Word, все в одном формате. Теперь я помещаю все эти страницы HTML для внутреннего справочного портала. Я думал о создании макроса, который при запуске автоматически генерирует действительный HTML-файл, который я могу напрямую загрузить на веб-сервер. Я могу получить все как HTML-код с помощью кода vba. Но я застрял в параграфах параграфа. Я получаю <Li></Li> теги для всех элементов списка, но как я могу получить тег или <ul> surronding them?Word 2 HTML VBA Macro

Sample документ доступен на 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

BTW Я знаю о сайтах преобразования 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 
Смежные вопросы