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 / August 2006

Tip: Looking for answers? Try searching our database.

error handling for find (.find) object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
akh2103@gmail.com - 28 Aug 2006 15:53 GMT
Hello--does anyone know how to do error handling for the find object? I
am searching for certain variable text in a word document (the variable
is set from user input in a user form) and I want to generate an error
handling routine for cases when the user types in text that is not in
the document. So I need to know how to do an error handling routine
that  checks to see if the find object exists and if it doesn't, I will
give them an error message and re-direct them to the user form. Any
help would be much appreciated. Thanks, Abe
Jay Freedman - 28 Aug 2006 16:55 GMT
It isn't really "error handling" because there isn't any error. There are
two ways to determine whether the requested text was found in the document.

(1) The property Selection.Find.Found or myRange.Find.Found will be True if
the requested text was found, and False if not.

(2) The Selection.Find.Execute or myRange.Find.Execute method returns a
boolean value that is the same as the .Found property.

Here are two code snippets to show how these are used. The "Else" clause is
often omitted because we don't want to do anything if the text wasn't found.

Sub foo1()
   Selection.HomeKey wdStory
   With Selection.Find
       .Text = "hello"
       .Execute
       If .Found Then
           Selection.Font.Bold = True
       Else
           MsgBox .Text & " was not found"
       End If
   End With
End Sub

Sub foo2()
   Selection.HomeKey wdStory
   With Selection.Find
       .Text = "fox"
       If .Execute Then
           Selection.Font.Bold = True
       Else
           MsgBox .Text & " was not found"
       End If
   End With
End Sub

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.

> Hello--does anyone know how to do error handling for the find object?
> I am searching for certain variable text in a word document (the
[quoted text clipped - 4 lines]
> and if it doesn't, I will give them an error message and re-direct
> them to the user form. Any help would be much appreciated. Thanks, Abe
 
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.