Help With Error Handling Please

G

Greg Maxey

I have code that I can't work out the error handling. It is used to find
text and replace with AUTOTEXT. There is an error handler that will detect
an incorrect process for inserting the AUTOTEXT when the dialog opens.
Basically you select the entry in the dialong and press insert. If OK or
Canel is pressed the handler displays a message and then starts again at the
beginning. All works well, the first time a User goons ups the insert.
However, I deal will very talented goons and if the Users messes up a second
time then a Run Time Error "The method or property is not available because
the object is empty."

I can't figure out 1) Why the existing error handler can't deal with this
and 2) How to code an error handler that will.

Thanks.

Sub ReplaceWithAUTOTEXT()

Dim FindText, ReplaceText As String

FindText = InputBox("Enter the text string you want to find", Find)
GetInput:
On Error GoTo Oops 'Handle incorrect AutoText request
'Create scratch pad
Documents.Add
Dialogs(wdDialogEditAutoText).Show
'Cut the inserted entry to the clipboard
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
'crumple up scratch pad :)
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

'Replace the requested text with the clipboard contents.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = FindText
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End
Oops:
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
MsgBox Prompt:="Reselect the autotext entry and click 'Insert'", _
Buttons:=vbExclamation, _
Title:="Incorrect autotext insertion"
GoTo GetInput
End Sub
 
J

Jezebel

I actually can't figure out your code (too much Chardonnay at this hour of a
a Saturday night) but the observations that leap out and kick one in the
teeth ...

First, the declaration is wrong (not the cause of your problem, but wrong
none the less)
Dim FindText, ReplaceText As String

This declares FindText as a variant, not a string. As a matter of coding
consistency, may I suggest that you put each declaration on its own line,
and use tabs to align the 'As' --

Dim FindText As string
Dim ReplaceText As String


But the critical issue is that your error-handling code MUST end with a
Resume statement -- otherwise VB thinks you are still within the original
error and subsequent errors cannot be handled by the same error-handler and
get passed up the line. There are three options: Resume, Resume Next, and
Resume [LineLabel]. In this case, you'll probably solve your problem by
replacing "Goto GetInput" with "Resume GetInput"


Yet another case of Goto causing havoc!
 
G

Greg Maxey

Jezebel,

Perfect. Thank you.

--
Greg Maxey
A peer in "peer to peer" support
Rockledge, FL
To e-mail, edit out the "w...spam" in (e-mail address removed)
I actually can't figure out your code (too much Chardonnay at this
hour of a a Saturday night) but the observations that leap out and
kick one in the teeth ...

First, the declaration is wrong (not the cause of your problem, but
wrong none the less)
Dim FindText, ReplaceText As String

This declares FindText as a variant, not a string. As a matter of
coding consistency, may I suggest that you put each declaration on
its own line, and use tabs to align the 'As' --

Dim FindText As string
Dim ReplaceText As String


But the critical issue is that your error-handling code MUST end with
a Resume statement -- otherwise VB thinks you are still within the
original error and subsequent errors cannot be handled by the same
error-handler and get passed up the line. There are three options:
Resume, Resume Next, and Resume [LineLabel]. In this case, you'll
probably solve your problem by replacing "Goto GetInput" with "Resume
GetInput"


Yet another case of Goto causing havoc!








Greg Maxey said:
I have code that I can't work out the error handling. It is used to
find text and replace with AUTOTEXT. There is an error handler that
will detect an incorrect process for inserting the AUTOTEXT when the
dialog opens. Basically you select the entry in the dialong and
press insert. If OK or Canel is pressed the handler displays a
message and then starts again at the beginning. All works well, the
first time a User goons ups the insert. However, I deal will very
talented goons and if the Users messes up a second time then a Run
Time Error "The method or property is not available because the
object is empty."

I can't figure out 1) Why the existing error handler can't deal with
this and 2) How to code an error handler that will.

Thanks.

Sub ReplaceWithAUTOTEXT()

Dim FindText, ReplaceText As String

FindText = InputBox("Enter the text string you want to find", Find)
GetInput:
On Error GoTo Oops 'Handle incorrect AutoText request
'Create scratch pad
Documents.Add
Dialogs(wdDialogEditAutoText).Show
'Cut the inserted entry to the clipboard
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
'crumple up scratch pad :)
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

'Replace the requested text with the clipboard contents.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = FindText
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End
Oops:
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
MsgBox Prompt:="Reselect the autotext entry and click 'Insert'", _
Buttons:=vbExclamation, _
Title:="Incorrect autotext insertion"
GoTo GetInput
End Sub



--
Greg Maxey
A peer in "peer to peer" support
Rockledge, FL
To e-mail, edit out the "w...spam" in (e-mail address removed)
 

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