E
Ed
I've just started working with ranges, so please bear with me. I use
Find.Execute to find a text string in a report. If it meets several
criteria (including in a table), I set endRange = Selection.End. Then I
move up to encompass the table caption, and set wkRange from the start of
the caption down to endRange, which should be at the end of my initial Found
text string. If another condition is met, I use wkRange.Collapse
wdCollapseEnd, which *should* take me back to the end of my initial text
string. So Selection.Homekey Line, Extend - which would select backwards
from the end of my range to the start of the line - *should* select my
original text string. Except ...
Stepping through the code, all went perfect until the wdCollapseEnd,
apparently. When selected, the text from the range end to beginning of the
line was nowhere near what it should have been! In fact, it looked like all
I selected was the beginning letter of my table caption, which is at the
very start of wkRange - unless I mangled the range when I set it.
So - any advice in setting and working with this range set up is greatly
appreciated. Also, is there any way to make the range visible as it is set,
expanded, or collapsed, so I can see what's happening?
Thanks much.
Ed
***********
Applicable code:
' If found, must be in Col 1 of a table
If Selection.Find.Found = True And _
Selection.Information(wdWithInTable) = True And _
Selection.Cells(1).ColumnIndex = 1 Then
' Set range to include caption
endRange = Selection.End
Selection.Tables(1).Cell(1, 1).Select
Selection.MoveUp Unit:=wdParagraph, Count:=3
Selection.HomeKey Unit:=wdLine
Set wkRange = Selection.Range
wkRange.SetRange wkRange.Start, endRange
' Set caption only as range
wkRange.Collapse Direction:=wdCollapseStart
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Set cRange = Selection.Range
' Check caption for "EFF"
With cRange.Find
.ClearFormatting
.Text = " EFF "
.Execute
End With
If cRange.Find.Found = True Then ' If caption contains "EFF"
wkRange.Collapse Direction:=wdCollapseEnd
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
If Selection.Text = TIR Then ' Cell contains only TIR No.
GotIt = True
End If
End If
End If
Find.Execute to find a text string in a report. If it meets several
criteria (including in a table), I set endRange = Selection.End. Then I
move up to encompass the table caption, and set wkRange from the start of
the caption down to endRange, which should be at the end of my initial Found
text string. If another condition is met, I use wkRange.Collapse
wdCollapseEnd, which *should* take me back to the end of my initial text
string. So Selection.Homekey Line, Extend - which would select backwards
from the end of my range to the start of the line - *should* select my
original text string. Except ...
Stepping through the code, all went perfect until the wdCollapseEnd,
apparently. When selected, the text from the range end to beginning of the
line was nowhere near what it should have been! In fact, it looked like all
I selected was the beginning letter of my table caption, which is at the
very start of wkRange - unless I mangled the range when I set it.
So - any advice in setting and working with this range set up is greatly
appreciated. Also, is there any way to make the range visible as it is set,
expanded, or collapsed, so I can see what's happening?
Thanks much.
Ed
***********
Applicable code:
' If found, must be in Col 1 of a table
If Selection.Find.Found = True And _
Selection.Information(wdWithInTable) = True And _
Selection.Cells(1).ColumnIndex = 1 Then
' Set range to include caption
endRange = Selection.End
Selection.Tables(1).Cell(1, 1).Select
Selection.MoveUp Unit:=wdParagraph, Count:=3
Selection.HomeKey Unit:=wdLine
Set wkRange = Selection.Range
wkRange.SetRange wkRange.Start, endRange
' Set caption only as range
wkRange.Collapse Direction:=wdCollapseStart
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Set cRange = Selection.Range
' Check caption for "EFF"
With cRange.Find
.ClearFormatting
.Text = " EFF "
.Execute
End With
If cRange.Find.Found = True Then ' If caption contains "EFF"
wkRange.Collapse Direction:=wdCollapseEnd
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
If Selection.Text = TIR Then ' Cell contains only TIR No.
GotIt = True
End If
End If
End If