PDA

View Full Version : Word VBA .Find (Attempt to explain its fickleness)



gmaxey
10-20-2012, 06:40 AM
Many of the regulars here are frequently offering explanations of how to work with Word VBA .Find.

I've put together and published some of my notes:

http://gregmaxey.mvps.org/word_tip_pages/words_fickle_vba_find_property.html

I'll be happy to revise it to correct any mistakes I may have made or add (within reason) any additional important points that some of you might want to include.

Thanks.

Paul_Hossler
10-20-2012, 02:15 PM
You do good writeups -- very clear and readable, screen shots and arrow really help

Lots of good stuff on your site.

Paul

PS. If you're looking for something to do, maybe you can do something to explain MS Word Ranges to us

fumei
10-20-2012, 02:35 PM
Word ranges are simple. They are the character spaces between two defined points - the Start and the End.

Oh...you mean how to USE them. Ah, not so simple. Especially since Word added StoryRanges.

I would also like to see what Greg would come up with for Ranges.

gmaxey
10-20-2012, 05:57 PM
Paul, Gerry,

Thanks. Before it try to explain things to others, I like to think that I understand it myself. I'm not 100% sure that I understand .Find, but felt that I was close enough to give it a shot.

Range on the ohter hand ... well maybe for distant rainy day. I think I've still got room to learn.

fumei
10-20-2012, 06:10 PM
A start (pun intended) may be (MAY be...) to explain the hierarchical nesting of:
ActiveDocument.Range.Tables(1).Range.Cells(2).Range.Paragraphs(2).Range


Then perhaps why:

With Selection

is in fact technically

With Selection.Range

gmaxey
10-21-2012, 05:31 AM
Gerry,

To start there, I would have to be able to explain:

Sub Test()
With ActiveDocument
.Tables.Add Selection.Range, 1, 3
.Tables(1).Range.Cells(2).Range.Text = "A" & vbCr & "B" & vbCr & "C"
Debug.Print
.Range.Tables(1).Range.Cells(2).Range.Paragraphs(1).Range.Select
.Range.Tables(1).Range.Cells(2).Range.Paragraphs(2).Range.Select
'Why doesn't the range to select ...
Debug.Print .Range.Tables(1).Range.Cells(2).Range.Paragraphs(3).Range.Start & " " & _
.Range.Tables(1).Range.Cells(2).Range.Paragraphs(3).Range.End
.Range.Tables(1).Range.Cells(2).Range.Paragraphs(3).Range.Select
'... equal the range selected.
Debug.Print Selection.Range.Start & " " & Selection.Range.End
End With
End Sub


While I have a general knowledge that the behavior observed, is due to the end of cell mark, I can't explain the mystery.

fumei
10-21-2012, 12:53 PM
For any testing this, try executing on a completely fresh document - i.e. the table Start is the very first character.

Now execute it again (with a fresh document) but with say three paragraph marks BEFORE the table.

Greg, the first debug does not appear to debug.print anything.

The end-of-cell marker is a unique, ummmm, thing. It is a double character Chr(13) and Chr(7) or bell...the "ding" that old typewriter made.

Now for even more weirdness...

In cell(2) - assuming the table is at the top of the document:
A (Range=1)
Chr(13) (Range=2)
B (Range=3)
Chr(13) (Range=4)
C (Range=5)
Chr(13) (Range=6)
Chr(7) (Range=7)

Dim r As Range
Set r = ActiveDocument.Range(Start:=6, End:=7)
r.Select
Debug.Print r.Start & " " & r.EndSelects the entire cell, even though the Start/End is 6 7. Thus it includes 1 to 5 as well.

OK? Now change it to:
Dim r As Range
Set r = ActiveDocument.Range(Start:=7, End:=7)
r.Select
Debug.Print r.Start & " " & r.End

Start and End are 7, which SHOULD be the last character in cell(2) - Chr(7). Yes? But where is the Selection??? It is in Cell(3) - not 2 - just before the end-of-cell marker.

Even though End is 7 (just like the previous), the Selection is now in a different cell altogether.

Are we having fun?

gmaxey
10-21-2012, 03:33 PM
Gerry,

Yes, the first debug.print was not supposed to be there.

Fun? Well, all I can say is that if anyone is ready to explain range, it isn't me ;-)

fumei
10-21-2012, 04:26 PM
The reason for the odd thing with selecting the last two characters (6 and 7) ending up selecting the entire cell - I think this was (sorta) your mystery - stems from a programming shortcut/laziness.

You know how in the GUI you can pobnt the cursor at the edge of a table cell, and therefore select the entire cell regardless of how big or long the cell is? Even if the cell extends over multiple pages, the entire cell can be selected.

That click programmatically selects the last two characters of the cell range, and they made THAT select the entire cell...regardless of the real start/end of the cell range. It is kind of a cheat (and not, as you note, an expected or sane result), but quite handy from a GUI point of view.

IMO it does add confusion, but AFAIK it is a singular piece of range weidness. I do not know of any other equivalent behaviour.

fumei
10-21-2012, 04:32 PM
Oh and the shift to cell(3) occurs because if a range start = end, a selection from that range is to the right of the last character. If the range covers >1 characters a selection made from that range includes the last character.

These sound like the same thing...but they are not. If a selection = IP technically speaking it is BETWEEN characters.