Word 2010 VBA - 操作编号列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8424573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word 2010 VBA - Manipulating Numbered Lists
提问by Kevin Pope
I'm trying to take a numbered list created in Outlook and manipulate it based on the top-level list items. Unfortunately, the only way I've found to manipulate the list is through the ListParagraph type, which breaks out all list items (including sub-items) equally, instead of having different access for each level in the list.
我正在尝试获取在 Outlook 中创建的编号列表,并根据顶级列表项对其进行操作。不幸的是,我发现操纵列表的唯一方法是通过 ListParagraph 类型,它平等地划分所有列表项(包括子项),而不是对列表中的每个级别具有不同的访问权限。
Is there a way to access, in one object, a list item, along with all its sub-items?
有没有办法在一个对象中访问列表项及其所有子项?
Thanks.
谢谢。
Here's what I'm currently using, which works fine for lists with only one level of items:
这是我目前正在使用的,它适用于只有一级项目的列表:
While i <= oMeetingWordDoc.Lists(1).ListParagraphs.Count
Set oRange = oMeetingWordDoc.Lists(1).ListParagraphs(i).Range
*Perform actions with oRange
i = i + 1
wend
By lists with 'one level' I mean something like this:
通过带有“一个级别”的列表,我的意思是这样的:
- Item 1
- Item 2
- Item 3
- 第 1 项
- 第 2 项
- 第 3 项
By lists with 'sub-items' I mean something like this:
通过带有“子项目”的列表,我的意思是这样的:
List item 1
a) Item a
b) Item b
c) Item cItem 2
a) Item a
b) Item bItem 3
a) Item a
列出项目 1
a) 项目 a
b) 项目 b
c) 项目 c第 2 项
a) 项目 a
b) 项目 b第 3 项
a) 项目 a
采纳答案by Drew Gaynor
ListFormat.ListLevelNumber
is what you're looking for. Here is some code that will output the list level and text of every ListParagraph
in the document:
ListFormat.ListLevelNumber
就是你要找的。下面是一些将输出ListParagraph
文档中每个的列表级别和文本的代码:
Sub listLevels()
Dim currentList As Range
Dim i, numLists As Integer
numLists = ActiveDocument.ListParagraphs.Count
For i = 1 To numLists
Set currentList = ActiveDocument.ListParagraphs(i).Range
MsgBox currentList.ListFormat.ListLevelNumber & " " & currentList.Text
Next
End Sub
You can of course use the condition of ListLevelNumber = 1
to access only top level lists, ListLevelNumber = 2
for second level, etc.
您当然可以使用条件ListLevelNumber = 1
来仅访问顶级列表、二级列表ListLevelNumber = 2
等。
Is there a way to access, in one object, a list item, along with all its sub-items?
有没有办法在一个对象中访问列表项及其所有子项?
I don't really think there's a great way to do this, unless you build it yourself using recursion or something (create an object with an array of children, and each child with it's own array of children, etc.). I don't have this coded up, but hopefully the code I posted will let you accomplish what you want to do- and it's much simpler.
我真的不认为有一个很好的方法可以做到这一点,除非你自己使用递归或其他东西来构建它(创建一个带有一组孩子的对象,每个孩子都有自己的孩子数组等等)。我没有这个代码,但希望我发布的代码能让你完成你想做的事情——而且它更简单。
Also, ListFormat
also has some other members that may be useful if you're doing a lot with lists- dig around in the Object Browser to learn more.
此外,ListFormat
还有一些其他成员,如果您经常使用列表,可能会很有用 - 在对象浏览器中挖掘以了解更多信息。
回答by jrichview
I have found the ListFormat.ListLevelNumber to be unreliable.
我发现 ListFormat.ListLevelNumber 不可靠。
I have a document someone sent me with a bulleted list that has a nested (level 2) list under one of the items. The nested list contains 3 subitems. Only subitem 2 reports that it is ListLevelNumber 2. The others continue to report ListLevelNumber = 1.
我有一份文件,有人发给我一个项目符号列表,其中一个项目下有一个嵌套(2 级)列表。嵌套列表包含 3 个子项。只有子项 2 报告它是 ListLevelNumber 2。其他项继续报告 ListLevelNumber = 1。
On a side note, the subitems that report the wrong list level have ListFormat.ListString set to the character used in level 2 of the list, so you might be able to work around the issue by checking both.
附带说明一下,报告错误列表级别的子项将 ListFormat.ListString 设置为列表级别 2 中使用的字符,因此您可以通过检查两者来解决此问题。