简体   繁体   English

如何在 MS Word 中使用 VBA 确定表格单元格中的段落是编号还是项目符号?

[英]How, using VBA in MS Word, do I determine if paragraphs in table cells are numbered or bulleted?

I need to find bulleted or numbered paragraphs that are in tables.我需要找到表格中的项目符号或编号段落。 How do I do that using VBA in MS Word documents?如何在 MS Word 文档中使用 VBA? I've tried looping through each paragraph:我试过循环遍历每一段:

For Each DocPara In ActiveDocument.Paragraphs
  Set rngPara = DocPara.Range
  If rngPara.Tables.Count > 0 Then
     Set lRange = rngPara.Tables.Item(1).Cell(1, 1).Range
    'How to test if cell contents has pargraph(s) that are bulleted or numbered?
  End if
Next DocPara

As you are only interested in bulleted or numbered paragraphs that are in tables it is rather inefficient to loop through all the paragraphs in the document.由于您只对表格中的项目符号或编号段落感兴趣,因此循环浏览文档中的所有段落效率很低。 Instead loop through all the tables and loop through the paragraphs in each table.而是遍历所有表格并遍历每个表格中的段落。

Dim para As Paragraph
Dim tbl As Table

For Each tbl In ActiveDocument.Tables
    For Each para In tbl.Range.Paragraphs
        With para.Range
            If Len(.ListFormat.ListString) > 0 Then
                'para is part of a list
            End If
        End With
    Next para
Next tbl

If you must process all the document's paragraphs there is a better way of determining whether a paragraph is in a table.如果您必须处理文档的所有段落,有一种更好的方法可以确定段落是否在表格中。

For Each DocPara In ActiveDocument.Paragraphs
  With DocPara.Range
    If .Information(wdWithInTable) Then
      'para is in table
    End if
  End With
Next DocPara
For Each DocPara In ActiveDocument.Paragraphs Set rngPara = DocPara.Range If rngPara.Tables.Count > 0 Then If Not rngPara.ListFormat.List Is Nothing Then 'is a list? Debug.Print rngPara.ListFormat.ListString End If End If Next DocPara

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

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