2

我查了一些例子,但我不太明白 Range 对象是如何工作的。我正在尝试遍历我的每个标题(第 4 级),并有一个嵌套循环查看标题之间的所有表格。我无法弄清楚如何设置该特定范围,因此将不胜感激任何帮助。

    Dim myHeadings As Variant
    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

    For iCount = LBound(myHeadings) To UBound(myHeadings)

      level = getLevel(CStr(myHeadings(iCount)))
      If level = 4 Then

         'This is where I want to set a range between myHeadings(iCount) to myHeadings(iCount+1)
         set aRange = ??


      End If

    Next iCount
4

2 回答 2

3

你在这里是正确的。您拥有的 myHeadings 变量只是给出了文档中第 4 级标题的字符串列表。然后,您需要做的是在文档中搜索这些字符串以获取 4 级标题的范围。

获得每个标题的范围后,您可以检查这些标题之间范围内的表格。我已经稍微修改了你的代码来做到这一点。Option Explicit另请注意将其放在模块顶部以确保声明所有变量的良好做法。

我的代码将告诉您每个 4 级标题之间有多少个表格。注意:它不会检查文档的最后一个标题和结尾之间,我将把它留给你;)

Sub DoMyHeadings()
    Dim iCount As Integer, iL4Count As Integer, Level As Integer, itabCount As Integer
    Dim myHeadings As Variant, tbl As Table
    Dim Level4Heading() As Range, rTableRange As Range

    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

    'We want to move to the start of the document so we can loop through the headings
    Selection.HomeKey Unit:=wdStory

    For iCount = LBound(myHeadings) To UBound(myHeadings)
        Level = getLevel(CStr(myHeadings(iCount)))
        If Level = 4 Then

            'We can now search the document to find the ranges of the level 4 headings
            With Selection.Find
                .ClearFormatting                                'Always clear find formatting
                .Style = ActiveDocument.Styles("Heading 4")     'Set the heading style
                .Text = VBA.Trim$(myHeadings(iCount))           'This is the heading text (trim to remove spaces)
                .Replacement.Text = ""                          'We are not replacing the text
                .Forward = True                                 'Move forward so we can each consecutive heading
                .Wrap = wdFindContinue                          'Continue to the next find
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute
           End With

           'Just make sure the text matches (it should be I have a habit of double checking
            If Selection.Text = VBA.Trim$(myHeadings(iCount)) Then
                iL4Count = iL4Count + 1                             'Keep a counter for the L4 headings for redim
                ReDim Preserve Level4Heading(1 To iL4Count)         'Redim the array keeping existing values
                Set Level4Heading(iL4Count) = Selection.Range       'Set the range you've just picked up to the array
             End If
         End If
     Next iCount

    'Now we want to loop through all the Level4 Heading Ranges
    For iCount = LBound(Level4Heading) To UBound(Level4Heading) - 1
        'Reset the table counter
        itabCount = 0

        'Use the start of the current heading and next heading to get the range in between which will contain the tables
        Set rTableRange = ActiveDocument.Range(Level4Heading(iCount).Start, Level4Heading(iCount + 1).Start)

        'Now you have set the range in the document between the headings you can loop through
        For Each tbl In rTableRange.Tables
            'This is where you can work your table magic
            itabCount = itabCount + 1
        Next tbl

        'Display the number of tables
        MsgBox "You have " & itabCount & " table(s) between heading " & Level4Heading(iCount).Text & " And " & Level4Heading(iCount + 1).Text
    Next iCount
End Sub
于 2013-02-06T23:09:45.270 回答
1

您可以使用 . 从一个标题跳到下一个标题Goto。请参阅下面如何循环遍历 4 级标题。

Dim heading As Range
Set heading = ActiveDocument.Range(start:=0, End:=0)
Do   ' Loop through headings
    Dim current As Long
    current = heading.start
    Set heading = heading.GoTo(What:=wdGoToHeading, Which:=wdGoToNext)
    If heading.start = current Then
        ' We haven't moved because there are no more headings
        Exit Do
    End If
    If heading.Paragraphs(1).OutlineLevel = wdOutlineLevel4 Then

        ' Now this is a level 4 heading. Let's do something with it.
        ' heading.Expand Unit:=wdParagraph
        ' Debug.Print heading.Text

    End If
Loop

不要专门寻找“标题 4”,因为,

  • 可以使用非内置样式,
  • 它不适用于 Word 的国际版本。

wdOutlineLevel4改为检查。

现在,要获得整个 4 级的范围,这里有一个鲜为人知的技巧:

Dim rTableRange as Range
' rTableRange will encompass the region under the current/preceding heading
Set rTableRange = heading.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")

这对于文档中的最后一个标题 4 或标题 3 下方的最后一个标题会更好。

于 2015-05-30T00:58:01.713 回答