Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

unable to search for style

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chip Orange - 05 Oct 2007 17:19 GMT
Hi All,

I'm having a problem with the following code failing to find something which
is in the document.  I know it's there because I can type similar code into
the immediate window and have it work (also below).

Fails:

DIM objRange as Word.Range

SET objRange = ActiveDocument.range
WITH objRange.Find
 .ClearFormatting
 .Text = "Issue"
 .Style = ActiveDocument.Styles("Issue Heading")
END WITH

IF objRange.Find.execute() then
...

and it fails.  If I type this into the immediate window, using the selection
object like so:

 selection.find.ClearFormatting
 selection.find.Text = "Issue"
 selection.find.Style = ActiveDocument.Styles("Issue Heading")
? selection.find.execute()

and that works! :( ?

I don't want to use the selection object in my macro for other reasons, and
this is a paired-down example so maybe I left something key out, but I don't
see any essential difference.  If I don't use style, but I use attributes
such as bold and underlining, then the first search example works ok, it's
only when searching for text in a specific style that it fails.

Any ideas?

Thanks.

Chip
Jay Freedman - 05 Oct 2007 18:25 GMT
Range objects and the Selection object behave differently with respect to
the "stickyness" of the parameters. A newly created Range object's .Find
parameters are all the defaults, while the Selection.Find inherits most of
its parameters from the most recent search that used the Selection
(including one done through the Edit > Find dialog).

In your code, include  .Format = True  to make it use the .Style parameter
instead of ignoring it. I'll guess that when you tried the Selection search
in the Immediate window, it inherited the .Format value from a previous
search using the dialog.

That said, I would have expected the Range search as you showed it to find
the word "Issue" in any text, regardless of its style -- that is, you might
get hits you don't want, rather than not getting hits you do want. Maybe
there's something else going on.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi All,
>
[quoted text clipped - 37 lines]
>
> Chip

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.