Results 1 to 12 of 12
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Turning off Error Trapping in Word (W2002SP-2)

    Is there a way to turn off Error Trapping using Word VBA -- i.e., a Word VBA equivalent to Access VBA's Application.SetOption "Error Trapping", 2?

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    Steve,

    Give us a chance! Your question as put requires an expert in both Word and Access VBA. If you tell us what <code>Application.SetOption "Error Trapping", 2</code> does then someone who knows the Word object model might be able to help you.

    StuartR

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    That instruction in Access does not turn off error trapping - it sets VB to break only on unhandled errors (as opposed to breaking on all errors.) As far as I know, Access is the only Office application that offers programmatic access to this setting. According to this newsgroup thread you would need to change some registry values (can be done in code) and force the user to reboot the system, so in practice, the answer seems to be "No".

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    Sorry, Stuart. I thought I'd described what it does when I referred to "turning off Error Trapping." But, as Hans points out, that's really a misdescription.

    In Access VBA, you can use the line (often in combination with On Error Resume Next) to ensure that certain lines of code that you know produce nonproblematic (or even productive) errors won't cause execution to break even if you've temporarily set Error Trapping to Break On All Errors because you're in "debug mode." Probably needless to say, you pair the first SetOption line with another SetOption line, after the nonproblematic lines of code, that resets the setting to whatever it was before the first SetOption line executed (e.g., to Break On All Errors if you were in debug mode).

    I take it from Hans's response (thanks, Hans) that, in Word (unlike Access), if you go into debug mode, there's no easy way to avoid having Word macros break on ALL errors. If there's nonproblematic-error code in a loop that's going to run 347 times before, on the 348th iteration, the problematic error you're trying to debug occurs, a boy could get kind of tired of hitting F5 (Continue).

    If anyone knows an alternative way of having Break On All Errors avoid selected lines in Word macro code, I'd love to hear about it.

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Turning off Error Trapping in Word (W2002SP-2)

    Sorry, Steve, but I don't see why you would want to do that even in Access. <img src=/S/confused.gif border=0 alt=confused width=15 height=20> The main use of debug mode is to identify errors, not ignore them.
    Charlotte

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    What are you trying to accomplish?

    <pre> On Error Resume Next
    </pre>


    will cause errors to be ignored.
    Legare Coleman

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    If "Break on All Errors" has been set in the Error Trapping section of the General tab of Tools | Options... in the Visual Basic Editor, the built-in error messages will be displayed even if error handling (On Error GoTo ... or On Error Resume ...) is present in the code. This was what Steve hoped to circumvent in code, but that is not possible, as far as I know.

    The default setting is "Break on Unhandled Errors", this will display the built-in error messages only if no error handling is present in the code.

  8. #8
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    Errors aren't always a bad thing, and can sometimes be put to productive use. For example, I learned a technique from the Access Developer's Handbook for determining the number of dimensions in an array. You set x to the highest number of dimensions you'd want to test for and loop down, and in each loop you refer to LBound(varArray, x). If varArray doesn't have that many dimensions, it generates Error 9 ("subscript out of range"), so you test for that error and move on to the next lower x if it's generated. Here's the code (as I use it in one of my procedures):

    Application.SetOption "Error Trapping", 2
    On Error Resume Next

    For x = 2 To 1 Step -1
    y = LBound(TargetArray, x)
    If Err <> 9 Then
    intArrayDimensions = x
    GoTo ExitLabel
    Else
    Err = 0
    End If
    Next x

    On Error GoTo 0
    Application.SetOption "Error Trapping", glngErrorTrapSwitch

    If I'm in debug mode under circumstances where this procedure is running, I don't want Access to break on these errors. (As I mentioned in my previous post, it's especially problematic if there's a loop in the code I'm debugging that will be calling this procedure many times in a row.)

    In Word, as another example, you can loop through the modules in a project until you find the one that contains a macro with a given name by using the following code:

    For Each vbcX In projRUW.VBComponents
    If vbcX.Type = vbext_ct_StdModule Then
    Set mdl = vbcX.CodeModule
    On Error GoTo 0
    lngX = mdl.ProcBodyLine(strName, vbext_pk_Proc)
    If lngX > 0 Then ...

    The ProcBodyLine line generates an error if the module doesn't have a macro with the given name, but that's an "intentional" error in this context (that just causes Word to move on to the next module), and I'd rather not have Word break on that error when I'm in debug mode. Apparently, however, that's not possible.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    You'll have to tell your users to set Error Trapping to "Break on Unhandled Errors". That is the preferred setting for end-users anyway, I wouldn't know why an end-user would ever set it to anything else. the "Break on All Errors" setting is only useful for developers (but I never use it, I just comment out error handling temporarily.)

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    Thanks again for your input.

    I'm my only user, which simplifies things tremendously. <img src=/S/yep.gif border=0 alt=yep width=15 height=15>

  11. #11
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    BTW, there's a simpler (?) or at least more straightforward way to determine the "rank" of an array (ie, number of dimensions) that does not rely on raising an error. A VB array is stored as a SAFEARRAY data structure that stores the number of dimensions in an array, the size of each element, etc. The "standard" typedef for a SAFEARRAY structure (array descriptor) in VB is provided in attached text file. Because an array, like a String or Object data type, is a pointer type, an array variable's address is actually a pointer to a pointer to an array descriptor, not to array descriptor itself (SAFEARRAY** in C++ syntax). VB does not support pointers, but provides various functions (VarPtr, StrPtr, etc) that can be used to return the address of a variable. See MSKB 199824 for a description of these functions, and MSKB 205277 for additional info:

    HOWTO: Get the Address of Variables in Visual Basic

    How to pass arrays and strings between Visual Basic and C functions or between Visual Basic and C++ functions by using Visual Basic 6.0

    Note the standard warning that these functions are undocumented, and not officially supported by Microsoft (ie, use at own risk). VarPtrArray returns the address of a SAFEARRAY structure, and can be used for any array variable (except for String arrays, which require VarPtrStringArray, which must be called via a type library to avoid "issues" due to automatic conversion of Unicode to ANSI when strings are passed from VB to C/C++). Both MSKB articles explain how to use MIDL to compile a simple type library for this purpose from the command line. Example:

    MIDL /t C:VB6ptrlib.odl /tlb C:WINDOWSSYSTEM32VB6ptrlib.tlb

    See 2nd article for details. Once compiled, set a reference to the PtrLib library (listed as "VarPtrStringArray Support for VB6"). The attached text file shows some examples of how to use these functions to get array descriptor information. Since VB doesn't support pointers or the C++ indirection operator (*), the CopyMemory function is used to copy a specified number of bytes from one memory address to another. For example, see GetDimCount function

  12. #12
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Turning off Error Trapping in Word (W2002SP-2)

    <<Perhaps using the intentional-raise-an-error approach is simpler in this case....>>

    <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15> Whoooeee. Ya, I think maybe ...

    However, I much appreciate your response and am filing it away for a day when I'm feeling particularly ambitious.

Posting Permissions

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