How to capture range from Content.Find?

E

Ed from AZ

I know that when I set a range and use Range.Find, the range is reset
to just the found text. I am working with documents that can be huge,
and want to save computer "head room". I thought that rather than
setting a range to the entire doc.Contents and using a Find wirth that
range, maybe I could just use doc.Contents.Find. But I can't seem to
capture the resulting range of the found text, because the Contents
range isn't going to reset.

I tried using
Set rngDoc = doc1.Content.Find.Execute(FindText:="MyText")
But it was a Type Mismatch error becuse the Execute returns a Boolean,
not a Range.

Is there a way to use a Find from the Contents object and capture the
range of the found text?

Ed
 
D

David Sisson

I know that when I set a range and use Range.Find, the range is reset
to just the found text.  I am working with documents that can be huge,
and want to save computer "head room".

I'm not sure what you mean by this, but if you want to shrink the size
of the search area of the document as you move down the document, then
use two ranges.

Sub SearchRange()
'From Writing Word Macros by Steven Roman
Dim RngToSearch As Range
Dim rngResult As Range

Set RngToSearch = ActiveDocument.Range
Set rngResult = RngToSearch.Duplicate

Do
With rngResult.Find
.ClearFormatting
.Text = "Find This Text"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute

'Check if something was found. If not, then exit.
If Not rngResult.Find.Found Then Exit Do

'Do somthing with your find.
rngResult.Select

'Prepare for next search by
'moving the start position over one word.
rngResult.MoveStart wdWord
'and extending the end of rngResult
rngResult.End = RngToSearch.End
Loop Until Not rngResult.Find.Found

End Sub
 
L

Lene Fredborg

I think an "End With" needs to be inserted after the line ".Execute".

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jay Freedman

I know that when I set a range and use Range.Find, the range is reset
to just the found text. I am working with documents that can be huge,
and want to save computer "head room". I thought that rather than
setting a range to the entire doc.Contents and using a Find wirth that
range, maybe I could just use doc.Contents.Find. But I can't seem to
capture the resulting range of the found text, because the Contents
range isn't going to reset.

I tried using
Set rngDoc = doc1.Content.Find.Execute(FindText:="MyText")
But it was a Type Mismatch error becuse the Execute returns a Boolean,
not a Range.

Is there a way to use a Find from the Contents object and capture the
range of the found text?

Ed

Hi Ed,

First, your plan won't work, and second, it isn't necessary anyway. :)

The reason it won't work: When you use the Find method of a Range object that
you declare, and the .Execute is successful, the Start and End of that Range
object are changed so the Range object contains only the found text. The Content
property of a document by definition refers to the entire document, and its
Start and End can't be changed.

The reason it isn't necessary: There is no "computer head room" to be saved. A
Range object is not a copy of the piece of document it refers to. It's simply a
bunch of pointers (addresses) in VBA's memory that tell it the Start and End
values and where to call for the various methods and properties. I'd be
surprised if it's more than a few hundred bytes.

So use a Range object and be happy!
 
E

Ed from AZ

Thanks, Jay. I was afraid of such.

The output of a certain report generator is a single text file with
all the requested documents strung end to end. I have a macro that
splits these up and save them as separate docs. The macro runs fine
on my computer, but bogs down on the machine of the person who
actually uses it!! She gets "Code Execution Has Been Interrupted"
errors in odd and irregular spots when the macro runs, both before and
during the looping. I thought it might simply be the macro having too
many objects and was lloking to save resource "haed room" by cutting
some things out. (I did find where I used two separate objects for
the FSO - never saw that one before!)

Okay then - I'll keep looking to slim this thing down. Thanks for the
input.

Ed
 
T

Tony Jollans

This seems to be poorly understood. You can code it but it doesn't buy you
anything.

Content is a Property of the Document Object and it returns a Range Object.

Every time you use the Content Property, a new Range Object is instanced and
set to content of the document. Once you have that Range you can use it like
any other *except* that it doesn't belong to you and you have no direct way
of addressing it. What you have to do is use a With construct so that Word
keeps the Range Object for you. When you do this, that Range Object is
automatically redefined by Find.Execute.

So you can do this:

With doc1.Content
' You now have a Range object you can use
End With
' The Range Object has now been discarded by Word

.. or this ..

With doc1.Content.Find
' You now have a Find object that ..
' .. you can use that will be redefined by .Execute
' (and its Parent is the Range object)
End With
' The Find (and Range) Object has now been discarded by Word

.. but you can't do this:

doc1.Content.Find.Execute(FindText:=etc
doc1.Content.somethingelse ' because this instances a new Range object


Each time your With block is run, a new Range object is brought into being
and a pointer to it held as long as necessary. Even in the best case you
really aren't gaining over just declaring your own Range object and using
that.

There are, of course, pros and cons to every way of doing things. With your
own Range, and child Find object, your settings within it are maintained.
New objects created by Word always have default values when they are
created. It depends what you want to achieve.
 
T

Tony Jollans

I just posted in response to your original and see you have now posted this.

The seemingly random "Code Execution Has Been Interrupted" breaks are
extremely annoying. I have experienced these myself in the past, and seen a
few reports of them in the newsgroups but have failed to find any reason for
them or any cure. Sometimes shutting down all open Office applications and
then restarting them works. Usually rebooting makes them go away. But they
do seem to have a tendency to return once they have happened once. I'd love
to hear positive results from your efforts but I suspect you are looking in
the wrong place - and I don't know where the right place is; sorry!

--
Enjoy,
Tony

Thanks, Jay. I was afraid of such.

The output of a certain report generator is a single text file with
all the requested documents strung end to end. I have a macro that
splits these up and save them as separate docs. The macro runs fine
on my computer, but bogs down on the machine of the person who
actually uses it!! She gets "Code Execution Has Been Interrupted"
errors in odd and irregular spots when the macro runs, both before and
during the looping. I thought it might simply be the macro having too
many objects and was lloking to save resource "haed room" by cutting
some things out. (I did find where I used two separate objects for
the FSO - never saw that one before!)

Okay then - I'll keep looking to slim this thing down. Thanks for the
input.

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