|
-
Nov 18th, 2005, 06:58 AM
#1
Thread Starter
Lively Member
[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...
-
Nov 18th, 2005, 08:20 AM
#2
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
Last edited by Hack; Nov 18th, 2005 at 10:34 AM.
-
Nov 18th, 2005, 10:24 AM
#3
Re: how to trap this error using error handling
You should not rely on the default property of the err object. Use Err.Number
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 18th, 2005, 10:26 AM
#4
Re: how to trap this error using error handling
 Originally Posted by RobDog888
You should not rely on the default property of the err object. Use Err.Number 
I did use Err.Number
-
Nov 18th, 2005, 10:27 AM
#5
Re: how to trap this error using error handling
..
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 18th, 2005, 10:34 AM
#6
Re: how to trap this error using error handling
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.
-
Nov 18th, 2005, 10:36 AM
#7
Re: how to trap this error using error handling
Bad Hack *SLAP* 
Its ok, I got slapped by wossname for comparing Static to a Public variable saying they were different.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 18th, 2005, 06:53 PM
#8
Thread Starter
Lively Member
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.
-
Nov 18th, 2005, 06:57 PM
#9
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. 
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".
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|