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
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