[RESOLVED] how to trap this error using error handling
The problem of this code is that if there is no word document opened then i have to used this program, i get an error "ActiveX component can't create object" Run-time error '429'. I want to trap this error using error handling if there is no word document opened but the problem is how to code this.
This is my code:
VB Code:
Private Sub Command1_Click()
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
Loop
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
End Sub
please solve my problem... i need your help... i want to trap this error if there is no document opened. all i need is if i clicked the commandbutton and there is no document opened... there is a message "you cannot start the counting operation, please opened a document"... but the problem.. how i am going to code that... please help...
Re: how to trap this error using error handling
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
Loop
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub
Re: how to trap this error using error handling
You should not rely on the default property of the err object. Use Err.Number ;)
Re: how to trap this error using error handling
Quote:
Originally Posted by RobDog888
You should not rely on the default property of the err object. Use Err.Number ;)
I did use Err.Number
Re: how to trap this error using error handling
Re: how to trap this error using error handling
:lol: Well, ain't I the hotshot programmer?
Put Err.Number in a flippin' messagebox and neglected to use it in my code. *Sigh*
Another glaring example of freehand typing gone astray.
Original post edited appropriately.
Re: how to trap this error using error handling
:lol: Bad Hack *SLAP* :D
Its ok, I got slapped by wossname for comparing Static to a Public variable saying they were different. :(
Re: how to trap this error using error handling
thanks for a quick reply... this forum is very useful to all who begin or who want to discover the visual basic.
Re: how to trap this error using error handling
Thanks and this is a great Forum as you need to stay around as your questions usually get responses within just a couple of minutes.
Thanks Hack for actually writting the error trapping code example. :D
As a newer member i would like to suggest that if your question has been answered satisfactorily if you could 'Resolve' your thread by going into the Thread tools menu and clicking "Mark thread as Resolved". ;)