[英]MS Word VBA to convert a Word document's nested list to an HTML nested list
Using MS Word VBA how can we convert the following nested list in a Word document to an html nested list. 使用MS Word VBA,我们如何将Word文档中的以下嵌套列表转换为html嵌套列表。 I know in Word-VBA ActiveDocument.Lists
is a collection of all lists and ListParagraphs
is a collection of list items in a list. 我知道在Word-VBA中, ActiveDocument.Lists
是所有列表的集合,而ListParagraphs
是列表中列表项的集合。 But I'm unable to loop through these collections to get a handle of the following nested list in Word document: 但是我无法遍历这些集合来获取Word文档中以下嵌套列表的句柄:
Word document nested list : Word文档嵌套列表 :
HTML nested List : HTML嵌套列表 :
<ol>
<li>Test1</li>
<li>Test2</li>
<ul>
<li>Test2a</li>
<li>Test2b</li>
</ul>
<li>Test3</li>
<li>Test4</li>
</ol>
UPDATE : 更新 :
Using code from user @TimWilliams
below I get the following result that actually treating sub-list as a separate list 2 (as shown below). 使用下面用户@TimWilliams
代码,我得到以下结果,该结果实际上将子列表视为单独的列表2(如下所示)。 But I need to know that the list 2 is indeed a nested list of list 1. How can I achieve this? 但是我需要知道列表2确实是列表1的嵌套列表。如何实现呢?
List: 1 Level: 1 Label: 1. Text: Test1
List: 1 Level: 1 Label: 2. Text: Test2
List: 1 Level: 1 Label: 3. Text: Test3
List: 1 Level: 1 Label: 4. Text: Test4
List: 2 Level: 2 Label: a) Text: Test2a
List: 2 Level: 2 Label: b) Text: Test2b
Should get you started: 应该让您开始:
Sub Tester()
Dim doc As Document, l As List, lp, i, x
Set doc = ActiveDocument
For x = 1 To doc.Lists.Count
Set l = doc.Lists(x)
For i = 1 To l.ListParagraphs.Count
Set lp = l.ListParagraphs(i).Range
Debug.Print "List: " & x, _
"Level: " & lp.ListFormat.ListLevelNumber, _
"Label: " & lp.ListFormat.ListString, _
"Text: "; Replace(lp.Text, vbCr, "")
Next i
Next x
End Sub
EDIT: opening a fresh document and running the code above on your example list, this is the output I get. 编辑:打开一个新文档并在您的示例列表上运行上面的代码,这是我得到的输出。
List: 1 Level: 1 Label: 1. Text: Test1
List: 1 Level: 1 Label: 2. Text: Test2
List: 1 Level: 2 Label: a. Text: Test2a
List: 1 Level: 2 Label: b. Text: Test2b
List: 1 Level: 1 Label: 3. Text: Test3
List: 1 Level: 1 Label: 4. Text: Test4
Seems like our lists somehow got created differently: I used tab to indent my sub-list items and Shift-tab to get back to the top-level list. 似乎我们的列表以某种方式创建了不同的方式:我使用tab缩进了子列表项,而使用Shift-tab返回了顶级列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.