Trouble collapsing range to end

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
 
G

Guest

I think as a rule of thumb you should always include the
word range when setting a range.

I didn't test your code but trying this:
endRange = Selection.Range.End
rather than
endRange = Selection.End

Good luck.
 
E

Ed

Thanks for the tip. It makes sense. I added it in, and then added in
wkRange.Select just to see if it would highlight it so I could see the
extent of the range. The Found text was in a cell in Col. 1 of a table, and
I would think the endRange would be set there at the end of the text. When
I selected it, though, the range went all the way from the beginning of the
caption (which is correct) to the end of that table row (which is *not*
correct).

Any ideas?

Ed
 
G

Guest

I did a little testing. If you try to do this manually you
get the same behavior. Place your cursor at the end of the
first cell of a table and then press the Shift key and the
up arrow. The entire row is selected. It appears to be the
way selection in a table behaves. Can you work your
problem out another way?
 
E

Ed

I think I can get it just using the Selection object instead of Range. If I
get stuck, I'll holler again! Thanks for your help.

Ed
 
P

Peter Hewett

Hi Ed

Sorry to cut into this thread but I think you have a problem with your first
IF statement:

If Selection.Find.Found = True And _
Selection.Information(wdWithInTable) = True And _
Selection.Cells(1).ColumnIndex = 1 Then

VBA If statements don't short circuit. In other words all the parts of an If
statement are always executed not just the parts until a True condition is
reached.

What this means for you is that if the text you are searching for (in the
code you haven't posted) finds the required text and that text is NOT in a
table then your if statement will fail with an error! The reason for this is
the "Selection.Cells(1).ColumnIndex" part of your If statement ALWAYS
executes. However, if you're not in a table then that statement will produce
an error - Since your refering to .Cells but there aren't any!

I posted you some code to do a Find over just the tables in a document in
another thread - if you use it then you wont encounter this problem. You can
then remove the "And Selection.Information(wdWithInTable) = True" part of the
If statement.

Is the caption you are trying to select in the 3 paragraphs just before the
table?

HTH + Cheers - Peter
 
E

Ed

Peter: I ran into the exact error you described and took several minutes to
figure out why "The required object of this collection does not exist".

I will always have the table caption on one line, then an empty paragraph,
and then the table. What I'm doing now is :
Selection.Tables(1).Cell(1, 1).Select
Selection.MoveLeft Unit:=wdWord, Count:=3
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend

This gets the caption selected so I can do a Find for a specific text string
that identifies the table. If that is correct, then I need to drop back to
where I was in the table. That was the reason for setting the range from
where I was up to include the caption: I could check both; if good, a
CollapseEnd would put me back where I needed to be. (In theory, anyway!
Hasn't worked yet.) I'm trying to work around my ignorance by setting a
bookmark in the table cell, then go up and check the caption; if good, go to
bookmark. We'll see what happens.

Thanks for all the help.
Ed
 
J

JGM

Hi Ed,

If the caption turns out to contains the required string, then instead of
doing a "CollapseEnd" to get back to where you were or using a bookmark...
consider the following:

Find the required text in the table;
Select it and define a range to include only that cell's text;
Check the table caption;
If you find what you need in the caption;
Select the previously defined range with something like:
MyRange.Select;
Do what you have to do with that text;
Set the range to nothing so as to be ready for the next table in the
document...

So this way you do not have to go to the caption, define a range extending
to the cell (which will actually include the complete row when you try to
select it later...)

I hope you can make sense of what I am writing here!

Cheers!
 
P

Peter Hewett

Hi Ed

I've tidied up you code. The attached code will:

1. Search all tables in a document (and ONLY tables) for the text "FindMe"
2. If the text is found in Column 1 of the table, it then:
3. Searches the Caption (the first of two paragraphs before the start of
the table being searched) for the text "EFF"
4. If the text if found a message box is diplayed.

This code uses Range objects rather than the selection object. Currently at
the time the message box is displayed the range object rngSearch maps the
text searched for. The range object rngCaption maps the paragraph
containing the Caption.

I hope this helps and makes sense. The code's clean and should be pretty
easy to modify.


Public Sub Crappo()
Dim tblTemp As Word.Table
Dim rngSearch As Word.Range
Dim rngCaption As Word.Range

' Do this for ALL tables in the document
For Each tblTemp In ActiveDocument.Tables

' Setup for the search
Set rngSearch = tblTemp.Range
With rngSearch.Find
.ClearFormatting
.Text = "FindMe"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Search ALL tables for the required text
If .Execute Then

' Was the text found in the tables 1st column
If rngSearch.Cells(1).ColumnIndex = 1 Then

' Now locate the Table Caption
Set rngCaption = rngSearch.Tables(1).Cell(1, 1).Range
rngCaption.MoveStart wdParagraph, -2
rngCaption.MoveEnd wdParagraph, -2

' Only interested if the Caption contains "EFF"
If InStr(1, rngCaption.Text, "EFF", vbTextCompare) Then
MsgBox "Caption contains EFF"
End If
End If
End If
End With
Next
End Sub

HTH + Cheers - Peter
 
E

Ed

Search only the tables? Oh, no - that's too simple! Why, that will cut
out half my "chewing gum and bailing wire" code and remove several IFs. I
*love* it!! Peter, give yourself a raise!

Thank you!
Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top