Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I code a macro to not take action if search string not found? 1

Status
Not open for further replies.

patchez

Technical User
May 17, 2002
3
US
My Word 97 macro searches for text, then takes some action. It may delete the found string and type more text or it may simply type more text. The macro may contain several (hundreds) find and replace instructions.

The problem: If the search string is not found, the new text is typed anyway.

How can I code the macro to not type the text if the search string is not found. I have used If Else successfully. However, I would like an overall solution, like OnError, so that I don't have to put an If Else at each search instruction. There doesn't seem to be an equivalent of WordPerfect's OnNotFound.

Does anyone have a solution?

Thanks so much

 
Hi, patchez,

You could use the IsError functoin. Assign the Found Value to a Variant variable. Then you next statement is ...
Code:
If IsError(FoundValue) Then
'   what to do if NO VALUE was found
...
Else
'   what to do if a value was found
...
End If
Hope this helps :) Skip,
metzgsk@voughtaircraft.com
 
Thank you for your answer. I will have to put the IsError at every search command, correct?

 
Well, is there a possibility that the search value will not be found and you might want to code for that possibility? I suppose that it is up to you... Skip,
metzgsk@voughtaircraft.com
 
What I'm trying to say is:

My macro will find, for example "loan policy", delete "loan policy" and type "loan application". THEN it finds "loan proceeds" and types a period and 5 more paragraphs.

If it doesn't find "loan policy" it just types "loan application" wherever the cursor happens to be. Likewise, if it doesn't find "loan proceeds", the rest of the text is inserted wherever the cursor happens to be.

What I want to do is to have a command at the beginning of the macro that says if search string not found, go to the next find instruction. I want to do it once and have it relate to every find instruction in that macro. If any of the search strings are not found, I want to display a message box that warns the user that the document did not assemble as expected.

Is that possible, or do you have to address each selection.find separately?

The reason I want this: I have a generic form that gets changed according to State language. The form may get edited. The edits to the form could possibly change the text the macro is looking for. Thus the problem. (Does this help explain my situation at all?)

Thank you



 
patchez,

It is possible to do several things. If you can logically determine what ought to be done if a value is not found, then execute that code.

Otherwise, you could set a general error flag whenever you encounter such a condition and simply report that somewhere in the document, things did no go as expected.

You could get a bit more specific and capture some information regarding the first or last search string that could not be found. Once a situation like this is encountered, is it worth going on or aborting the procedure? Taking it a step further, you could build and array of search values that could not be found and report those value(s) at the end of the process.

Does this help? Skip,
metzgsk@voughtaircraft.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top