Results 1 to 9 of 9

Thread: [RESOLVED] how to trap this error using error handling

  1. #1

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Resolved [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:
    1. Private Sub Command1_Click()
    2.  
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20.  Loop
    21.  
    22. End With
    23. MsgBox "There are " & count & " words found in this document", vbInformation
    24. Set objWdRange = Nothing
    25. Set objWdDoc = Nothing
    26.  
    27.  
    28. 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...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to trap this error using error handling

    VB Code:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20.  Loop
    21.  
    22. End With
    23. MsgBox "There are " & count & " words found in this document", vbInformation
    24. Set objWdRange = Nothing
    25. Set objWdDoc = Nothing
    26. Exit Sub
    27. ErrTrap:
    28. If Err.Number = 429 Then
    29.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    30. Else
    31.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    32. End If
    33. End Sub
    Last edited by Hack; Nov 18th, 2005 at 10:34 AM.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: how to trap this error using error handling

    If Err = 429 Then
    ..
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    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.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width