+ Reply to Thread
Results 1 to 11 of 11

How to retrieve Error Trapping options in VBA?

  1. #1
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    How to retrieve Error Trapping options in VBA?

    Hello!

    How can we "read" information from the VBE Error Trapping options (Break on All Errors/ Break in Class Module/ Break in Unhandled Errors) programmatically?

    Background: I am developping a spreadsheet that needs to be sent around to distant users. Some of them might not have the default "Break on Unhandled Errors" setting), which would just make my programme fail or risk to fail...

    This issue has been covered in the thread below, but the proposed solution doesn't work on my side (always returning the same output!):
    http://www.excelforum.com/excel-prog...s-excel-2.html

    Hint: it has been suggested to look into the registry, which the linked post does.

    Ideally, I would love to be able to change the setting programatically, but that seem near to impossible in excel 2007 VBA (unless calling an external programme that closes and restarts excel after having changed the option?...)

    Thanks in advance for your answers, ideas and, not the least, your time! :-)

    Phil

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: How to retrieve Error Trapping options in VBA?

    Can you make the change then force close excel with a message to the user to reopen file as a setting needs to be updated?
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to retrieve Error Trapping options in VBA?

    Thanks Mike,
    That's a good idea, but do you know how to implement it?
    I already have problems looking for the settings. It involves reading in the registry. If it is also possible to write in the registry from VBA, then your idea would be perfect. But how (I unfortunately have no clue on the way the registry works...)?...

  4. #4
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to retrieve Error Trapping options in VBA?

    The macro below is supposed to solve the problem, but doesn't work for me. Does somebody have an explanation? Thanks!


    Private Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

    Private Declare Function RegOpenKey Lib "advapi32.dll" _
    Alias "RegOpenKeyA" (ByVal hKey As Long, _
    ByVal lpSubKey As String, phkResult As Long) As Long

    Private Declare Function RegSetValueEx Lib "advapi32.dll" _
    Alias "RegSetValueExA" (ByVal hKey As Long, _
    ByVal lpValueName As String, ByVal Reserved As Long, _
    ByVal dwType As Long, lpData As Any, _
    ByVal cbData As Long) As Long

    Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
    Alias "RegQueryValueExA" (ByVal hKey As Long, _
    ByVal lpValueName As String, ByVal lpReserved As Long, _
    ByRef lpType As Long, lpData As Any, _
    ByRef lpcbData As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Const REG_DWORD = 4
    Const HKEY_CURRENT_USER = &H80000001

    Public Enum ErrorHandlingStatus
    AllErrors = 1
    ClassErrors = 2
    UnhandledErrors = 3
    End Enum
    Function GetErrorHandlingState() As ErrorHandlingStatus
    Dim hKey As Long, lngReturn As Long, lngDataLen As Long, lngKeyType As Long, lngCurrent As Long, success As Long
    Dim varValue, varReturn
    Dim strReturn As String

    Const strAllErrors As String = "BreakOnAllErrors"
    Const strClassErrors As String = "BreakOnServerErrors"
    Const HKEY_CURRENT_USER = &H80000001

    lngKeyType = REG_DWORD

    ' default state
    GetErrorHandlingState = UnhandledErrors

    RegOpenKey HKEY_CURRENT_USER, _
    "Software\Microsoft\VBA\6.0\Common", hKey

    If hKey > 0 Then
    ' check for all errors
    lngCurrent = RegQueryValueEx(hKey, strAllErrors, 0, lngKeyType, lngReturn, 4)
    If lngCurrent = 0 Then
    If lngReturn = 1 Then
    GetErrorHandlingState = AllErrors
    Exit Function
    End If
    End If
    ' check for class errors
    lngCurrent = RegQueryValueEx(hKey, strClassErrors, 0, lngKeyType, lngReturn, 4)
    If lngCurrent = 0 Then
    If lngReturn = 1 Then
    GetErrorHandlingState = ClassErrors
    Exit Function
    End If
    End If
    RegCloseKey hKey
    End If

    End Function

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: How to retrieve Error Trapping options in VBA?

    Wouldn't you just be better off handling the errors properly in your code to begin with?

  6. #6
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to retrieve Error Trapping options in VBA?

    Kyle,
    The errors are handled! Just, if the users have modified the error trapping settings, the error handlers won't be called...

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: How to retrieve Error Trapping options in VBA?

    Yes they will, for the below it doesn't matter what the user has selected:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to retrieve Error Trapping options in VBA?

    Agreed for the cases when "Break on Unhandled Errors" and "Break in Class Module" are selected.

    But your code gives an error if the VBE setting is set to "Break on All Errors": "Run-time error '11': Division by zero"

    You will tell me that users who have changed these settings should (normally) be able to change them back. But having a more generic solution that at least detects the settings and instruct the users to change them manually would be nice

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: How to retrieve Error Trapping options in VBA?

    My advice is don't. Any user who is sufficiently advanced enough to have changed that setting will not thank you for faffing with his dev set up and you will cause irritation Worry about the 99% of users who don't know what they are doing - not the 1% who will know what the issue is and how to resolve it.

  10. #10
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to retrieve Error Trapping options in VBA?

    Ok, wise point of view

    Without modifying any setting, wouldn't it be nice though to just warn the (advanced) users that their settings need to be at least temporarily modified to allow smooth operation of the application?

  11. #11
    Registered User
    Join Date
    09-10-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to retrieve Error Trapping options in VBA?

    @Kyle
    Finally, I'll adopt your solution and will just warn the users in a separate "manual". Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Tools > Options > General > Error Trapping
    By XmisterIS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-16-2014, 02:47 PM
  2. Error Trapping
    By yunesm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2010, 05:51 PM
  3. Trapping error in add-in
    By webbug08 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-05-2009, 05:04 AM
  4. Error trapping
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2006, 08:30 AM
  5. error trapping
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-04-2005, 08:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1