Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    On Error - Word (Word 2000)

    I have my users enter a directory into which they want to save a file. If they enter an invalid path, I want to capture that.
    I use on error to do this, but when an error occurs it zaps through my error code without pausing at the msgbox command.
    Here's the code:

    On Error GoTo Filenam

    'Then, when the error occurs:
    Filenam:
    MsgBox "The Directory path contains errors. Please have your Administrator check the template", vbOKOnly, "Path Error"
    Unload RI 'This unloads the RI form
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

    Even when I rem out the unload and Activedoc... commands, the msgbox appears then disappears almost immediately.
    What is wrong?

    Thanks for your help!

  2. #2
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error - Word (Word 2000)

    By the way, the "path error" section of the msgbox command is all on the same line.

  3. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error - Word (Word 2000)

    instead of having them typing in the path, let them select it through a dialog:

    <pre>Sub BrowseForFolderorCreateAFolder()
    Dim GetFolder As String
    GetFolder = _
    CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Select folder to save a file to", 0, &H11). _
    Items.Item.Path
    MsgBox GetFolder
    End Sub
    </pre>


    this code has been posted earlier in this forum a few months/weeks ago.

  4. #4
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error - Word (Word 2000)

    Pieter, thanks,

    The problem is that the administrator doesn't want the users to select where the files are saved - the danger being that they could then be saved all over the place. Each Admin. who uses the template wants to go into the code, change the path there, then save and distribute the template to their users, hence the need for the path to be hardcoded.

    I can't see why, if the admin enters an invalid path, such as c;data, or accidentally leaves out a double quote, why the on error section of my code rushes through and does not stop with the msgbox command.

    Thanks however for the useful code - I'll keep that in mind for other projects!

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: On Error - Word (Word 2000)

    As far as I know, a MsgBox is always "application modal"; accordingly, I don't see how your procedure can run past that line.

    I wonder whether you need to reset your error handling somewhere along the way. If you read about the Resume statement, you will find various suggestions that the On Error Goto Label might not run the second time unless you Resume at the end of the first error handler. Just a thought, since I haven't seen the full code. Here's an old post on the subject of resetting error handling:
    <hr>Subject: Re: Word Error Handling (Word 97 SR2) [#209290]
    Poster: jscher2000
    Posted on: 30-Dec-02 21:59
    <UL>Subject: Re: Word Error Handling (Word 97 SR2) [#209251]
    Poster: Kevin
    Posted on: 30-Dec-02 21:05

    The goto -1 resets the error handling so that you can trap exceptions in your error handlers and so you can nest error handlers.
    As far as I know, it's supported from VB5 thru VB.Net

    Kevin
    [/list]That one's not in my "...in a Nutshell" book. Here are some references for future, uh, reference:

    Visual Basic Language Reference - On Error Statement
    Visual Basic Language Specification - 8.11.2.2 On Error Statement

    These sources suggest that using On Error GoTo -1 clears the outstanding error condition, which is a bit more than clearing the error handler, but as a practical matter, a seemingly necessary action before changing it. I'm struggling to think of a situation in which I would use it, since a Resume Next statement at the end of an error handler has the same effect in my test:

    Sub ErrorTester()
    Dim intCount As Integer
    intCount = 1
    On Error GoTo errHandler1
    MsgBox intCount / 0
    intCount = intCount + 1
    MsgBox intCount / 0
    On Error GoTo errHandler2
    intCount = intCount + 1
    MsgBox intCount / 0
    On Error GoTo -1
    intCount = intCount + 1
    MsgBox intCount / 0
    On Error GoTo errHandler3
    intCount = intCount + 1
    MsgBox intCount / 0
    LastLabel:
    intCount = intCount + 1
    ' Because we got here without a Resume, the next division
    ' error is not handled unless previous error is canceled
    ' Err.Clear 'does not reset error condition/handler
    ' On Error GoTo -1 'causes infinite loop, due to hardcoded GoTo
    Stop
    MsgBox intCount / 0
    Exit Sub

    errHandler1:
    Debug.Print "Test " & intCount & ": invoked errHandler1"
    Resume Next
    errHandler2:
    Debug.Print "Test " & intCount & ": invoked errHandler2"
    Resume Next
    errHandler3:
    Debug.Print "Test " & intCount & ": invoked errHandler3"
    ' This next line does not reset error handling...
    GoTo LastLabel
    End Sub<hr>
    Hope this helps somehow. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

  6. #6
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error - Word (Word 2000)

    Thanks, jscher,

    Iagree - I don't see how it can run past the msgbox command either. Makes me think something else is going on somewhere. As you suggest, a thorough going over of the tempate - and the normal template as well - is in order!

    Thanks for your input - and for the links. Much appreciated.

Posting Permissions

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