Word 2010 VBA - 操作编号列表 [英] Word 2010 VBA - Manipulating Numbered Lists

查看:136
本文介绍了Word 2010 VBA - 操作编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取在 Outlook 中创建的编号列表,并根据顶级列表项对其进行操作.不幸的是,我发现操纵列表的唯一方法是通过 ListParagraph 类型,它平等地划分所有列表项(包括子项),而不是对列表中的每个级别具有不同的访问权限.

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.

有没有办法在一个对象中访问列表项及其所有子项?

Is there a way to access, in one object, a list item, along with all its sub-items?

谢谢.

这是我目前正在使用的,它适用于只有一级项目的列表:

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:

  1. 项目 1
  2. 项目 2
  3. 第 3 项

对于带有子项目"的列表,我的意思是这样的:

By lists with 'sub-items' I mean something like this:

  1. 列表项 1

  1. List item 1

a) 项目 a
b) 项目 b
c) 项目 c

a) Item a
b) Item b
c) Item c

项目 2

a) 项目 a
b) 项目 b

a) Item a
b) Item b

项目 3

a) 项目 a

a) Item a

推荐答案

ListFormat.ListLevelNumber 正是您要找的.下面是一些将输出文档中每个 ListParagraph 的列表级别和文本的代码:

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:

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

你当然可以使用ListLevelNumber = 1的条件来只访问顶级列表,ListLevelNumber = 2 用于二级等

You can of course use the condition of ListLevelNumber = 1 to access only top level lists, ListLevelNumber = 2 for second level, etc.

有没有办法在一个对象中访问列表项及其所有子项?

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.

此外,ListFormat 还有一些其他成员,如果您经常使用列表,可能会很有用 - 在对象浏览器中挖掘以了解更多信息.

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.

这篇关于Word 2010 VBA - 操作编号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆