Results 1 to 3 of 3

Thread: catch a fault in VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Location
    Poland
    Posts
    10

    catch a fault in VBA

    How can I catch a fault connected with open file ( if file dosn't exist )

    Workbooks.Open( .....)

    Is there something like "try .... catch" as in c++ ?

    thank for help

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: catch a fault in VBA

    In the place where you'd use Try..., put On Error GoTo errFoo (call errFoo whatever you want). Then, at the end of the code in the sub, put in a line saying Exit Sub (this prevents the error handling code that comes next from running when there are no errors).
    Next line enter errFoo: (with the colon, this indicates a label). Under that, put whatever error handling code you want. You can enter Resume to go go back and try to execute the code from the line that caused the error, Resume Next to start with the line following the error causing line, or just exit the sub.
    VB Code:
    1. Private Sub Foo()
    2.   On Error GoTo errFoo
    3.  
    4.   'code here
    5.   'code here - throws an error
    6.   'code here - start here with Resume Next
    7.  
    8.   errFoo:
    9.   msgbox Err.Description
    10.   Resume Next
    11. End Sub
    Tengo mas preguntas que contestas

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: catch a fault in VBA

    you can use the Dir function first to see if the file exists

    if not len(Dir(pathfilename)) = 0 then
    open pathfilename for whatever
    end if

    pete

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