Page 1 of 3 123 LastLast
Results 1 to 15 of 42

Thread: On Error or???

  1. #1
    kelliel
    Guest

    On Error or???

    Some have expressed the reluctance to use On Error. Personally, I like to leave it as a very last resort.

    Having said that, I find myself in the position that I need to programmatically change the directory for the user. I can do that in Word97SR2 VBA with the "ChangeFileOpenDirectory" followed by the directory.

    What if the directory doesn't exist? Error code 4172 comes up.

    I can certainly trap it with something like:

    <pre> On Error Resume Next
    ChangeFileOpenDirectory mstrDefineDocumentPathway
    If Err.Number = 4172 Then
    perform my task
    End If
    On Error GoTo 0
    </pre>


    But, might there be a more "elegant" way? Is there even another way?

    Thanks

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    Well, On Error Resume, etc. is ugly, but in a friendly sort of way. It can save a lot of error code when it's not really needed.

    You could however do this:

    if len(Dir(mstrDefineDocumentPath,vbDirectory)) = 0 then
    'Directory does not exist
    else
    'It does
    endif
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    kelliel
    Guest

    Re: On Error or???

    Cooooo-ool!

    Exactimundo!

    Thanks. <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    Another happy customer.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: On Error or???

    Here's another way (which I've seen in a few places, most recently an article by Romke Soldaat in the current OfficePro magazine):

    Function FileExists(strFilename)
    On Error Resume Next
    Call GetAttr(strFilename)
    FileExists = (Err = 0)
    Err.Clear
    End Function

    According to VBA help, the GetAttr function is applicable to files, directories or folders.

    It's sort of ironic, in the context of the original post, that this function depends on an "On Error Resume Next" statement <g>.

    By the way, this function illustrates the use of a logical assignment (which is item 3 in the Ken Getz article referred to by Xiao Bin in another thread).

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    Funny you should mention that. I'm currently rewriting my 'Files" code, producing a rather neat (and eleborate) developer function called FDir, a replacement for VBA's DIR function.


    I keep running into minor obstacles, and came across this one half an hour ago.


    I have too much time on my hands, so thought I'd browse the lounge .... (grin!)


    Here's something else I learned about something, but I'm not sure what:

    The first example below works, the second doesn't.


    It's *as if* there are TWO calls being made to the GetAttr function, and the first is somehow using up the On Error. Regardless, the statement fails with the Bad File error I reported earlier today (I made a directory c:temp...erase)

    By storing the calculated Integer value from GetAttr in a local variable, the On Error seems to do its job.

    I'm not well versed in On Error, and habitually mistreat the beast, so this posting is more to corral anyone else who searches for leads in problems with the same.


    <pre> ' Files with names like "...eras" give us trouble so ...
    On Error GoTo badfile
    Dim intAttributes As Integer
    intAttributes = GetAttr(strDrives & strPaths & strResult)
    While (strResult <> "") And ((intAttributes And intMasks) <> intMask)
    strResult = Dir
    badfile:
    On Error GoTo badfile
    Wend
    On Error GoTo 0
    </pre>




    <pre> On Error GoTo badfile
    While (strResult <> "") And ((GetAttr(strDrives & strPaths _
    & strResult) And intMasks) <> intMask)
    strResult = Dir
    badfile:
    On Error GoTo badfile
    Wend
    On Error GoTo 0
    </pre>


  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    Maybe this would make a good challenge in the VB/VBA forum: Finding ways to avoid ALL uses of On Error.

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    Personally, I wouldn't want to get rid of all uses. I still want it to trap real errors, and to signal- or even log- them.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #9
    kelliel
    Guest

    Re: On Error or???

    As I stated earlier, I use On Error as a last resort. And I would include your reasons, Geoff, in my "last resort" statement.

    When something real happens, we do need to know.

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    If you want to do more than just "Does a file exist", you might want to look at scripting. Create a reference to "Microsoft Scripting Runtime", and define a variable as "FileSystemObject" (I think you have to have Office 2000 or VB6 though). It gives you the ability to check drives, copy folders, get a drive name etc.

    Here's <A target="_blank" HREF=http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbenlr98/vaobjfile.htm>one reference</A>- but set up slightly differently.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    kelliel
    Guest

    Re: On Error or???

    Is the Microsoft Scripting Runtime something that can be network based and doesn't require registering, like a dll?

    I can't really use anything that would require individual installation on the users' computers; or, more accurately, I want to keep that to a real minimum--like never. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  12. #12
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    Lawrence,

    The scripting runtime refers to scrrun.dll- on C:winntSystem32 on my machine. If you don't have that dll from Office 2000 then you may need to distribute that file (I see you have Word 97 so it may be a problem).

    If ALL you're doing is to check if a file exists, don't worry about it at all. <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=vb&Number=34357&page=0& view=expanded&sb=5&vc=1#Post34357>Kevin's post</A> has a better way of checking for a file.

    I'm sure there's other ways of doing the extra tasks as well. Scripting runtime just provides them fairly elegantly.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    I agree with trapping/logging/dealing with all errors.

    I don't agree with "On Error"; On Error is a statement "I don't know what I'm doing, but I know it fails, so I'll dump the problem here".


    Now that I know that Win95 allows c:temp...erase as a directory name but Word97VBA GetAttr chokes, I *ought* to write a test for file names (returned by the DIR function) that start with period and are not "." or "..". Here's another case where "On Error" is lazy programming, and can actually cause us to gloss over more serious errors.

  14. #14
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: On Error or???

    If I didn't include error handling in the code I write at work, I would get booted off the team.

    And I agree with our in-house policy: particularly with a large userbase, you can never test/anticipate all possible errors.

    And if and when errors occur, the worst thing you can do is leave the user with an untrapped error.

    Besides allowing you to log errors (even if only by means of a relevant MsgBox that the user can report to help desk), On Error gives you the means to gracefully exit the procedure if necessary, shut down/undo what needs to be shut done/undone etc.

  15. #15
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error or???

    I'd agree with that.

    Errors will always occur. If I distributed a template, and a user found a problem, error handling can help me to identify the error much more quickly.

    A little while ago, I had a look at an application- something like "Under the Hood"? There was an untrapped error (I think); I was able eventually to track down what data was causing the error- but I could not provide the author with any info about where the error was occurring.

    Whoops, sorry Chris, was that your code? <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •