G
Greg Maxey
This may have been addressed before but I couldn't find anything with a
Google search of the VBA groups.
I can't seem to limit the range of a find operation to a single section of a
document (without using blunt force). To illustrated, create a new blank
document with three sections. In each section type the single word "Test."
Run this code:
Sub ScratchMacro()
Dim oRng As Range
Dim i As Long
Dim oCnt As Long
For i = 1 To ActiveDocument.Sections.Count
oCnt = 0
Set oRng = ActiveDocument.Sections(i).Range
With oRng.Find
.Text = "Test"
.Wrap = wdFindStop 'Makes no difference
While .Execute
'If oRng.InRange(ActiveDocument.Sections(i).Range) Then 'Blunt force
oCnt = oCnt + 1
'End If 'Blunt force
Wend
End With
MsgBox "Found " & oCnt & " times in Section " & i
Next i
End Sub
With the "Blunt force" lines stetted out the macro returns 3, 2, then 1 vice
the expected 1, 1, and 1.
Why is oRng being expanded beyond the "Set" scope?
Thanks.
Google search of the VBA groups.
I can't seem to limit the range of a find operation to a single section of a
document (without using blunt force). To illustrated, create a new blank
document with three sections. In each section type the single word "Test."
Run this code:
Sub ScratchMacro()
Dim oRng As Range
Dim i As Long
Dim oCnt As Long
For i = 1 To ActiveDocument.Sections.Count
oCnt = 0
Set oRng = ActiveDocument.Sections(i).Range
With oRng.Find
.Text = "Test"
.Wrap = wdFindStop 'Makes no difference
While .Execute
'If oRng.InRange(ActiveDocument.Sections(i).Range) Then 'Blunt force
oCnt = oCnt + 1
'End If 'Blunt force
Wend
End With
MsgBox "Found " & oCnt & " times in Section " & i
Next i
End Sub
With the "Blunt force" lines stetted out the macro returns 3, 2, then 1 vice
the expected 1, 1, and 1.
Why is oRng being expanded beyond the "Set" scope?
Thanks.