gpt4 book ai didi

vba - 字 VBA : Get Range between Consecutive Headings

转载 作者:行者123 更新时间:2023-12-02 22:14:16 24 4
gpt4 key购买 nike

我查了一些例子,但我不太明白 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

最佳答案

您在这里走在正确的轨道上。您拥有的 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

关于vba - 字 VBA : Get Range between Consecutive Headings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14737107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com