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

    Procedure Name Where Error Occurred (Word 97SR2)

    Not too long ago we toiled with efforts to get the sub/func
    name at which an error occurred in order to display a more
    useful error dialog that pinpoints an error.

    I've gained some ground on topic and would like some feedback, advice, etc.

    Here are two procs (and a test calling sub) used in my
    error reporting dialog that captures the sub name. If you
    have line numbers turned on for the codepane or proc, you
    should get the line number as well (though I haven't tested
    that lately).

    What you see below is the userform populated and
    presented to the user so they can submit the error report
    via email. Attached to the email is a screenshot taken
    before the dialog pops up.

    To test, you'll have to make a userform with the named controls.


    <pre>Function GetSubName(ByVal UniqueStr As String, ByVal strMod As String) As String
    Dim Line As Long
    Dim oMod As CodeModule
    Line = 1
    Set oMod = VBE.ActiveVBProject.VBComponents(strMod).CodeModul e
    With oMod
    If .Find(UniqueStr, Line, 1, .CountOfLines, 1) Then
    GetSubName = .ProcOfLine(Line, 0) ' 0 = vbext_pk_Proc
    End If
    End With
    End Function


    Sub DisplayErrorTest()
    Dim ModName As String
    Dim strMsg As String
    vErrorMsg = "The only restriction is the error message must be unique."
    vErrorSeverity = "Critical"
    Call DisplayError(vErrorMsg, VBE.ActiveCodePane.CodeModule, vErrorSeverity, Erl())
    End Sub
    '
    'Sets up then calls the userform
    Sub DisplayError(strMsg$, strMod$, strSeverity$, strERL$)
    Dim strSub As String
    strSub = GetSubName(strMsg, strMod)
    Call ScreenToClipboard

    With frmErrorForm
    .txtErrorMsg = strMsg & vbNewLine & _
    "Error Number: " & CStr(Err.Number) & ";
    Description: " & Err.Description & vbNewLine & _
    "Module: " & strMod & vbNewLine & _
    "Subroutine: " & strSub & vbNewLine & _
    "Severity: " & strSeverity & vbNewLine & _
    "Line Number: " & strERL

    Select Case strSeverity
    Case "Critical"
    .imgCritical.Left = 25: .imgCritical.Top = 45
    '.txtErrorMsg = .txtErrorMsg & "Critical. Cannot Proceed."
    Case "Warning"
    .imgWarning.Left = 25: .imgWarning.Top = 45
    '.txtErrorMsg = .txtErrorMsg & "Warning. Proceed with caution."
    Case "StopLight"
    .imgStopLight.Left = 25: .imgStopLight.Top = 45
    '.txtErrorMsg = .txtErrorMsg & "Could be trouble."
    Case "Dynamite"
    .imgDynamite.Left = 25: .imgDynamite.Top = 45
    '.txtErrorMsg = .txtErrorMsg & "I smell plastic burning..."
    Case Else
    .imgWarning.Left = 25: .imgWarning.Top = 45
    '.txtErrorMsg = .txtErrorMsg & "Warning. Proceed with caution."
    End Select
    .Show
    End With
    End Sub

    </pre>

    Attached Images Attached Images
    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>

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

    Re: Procedure Name Where Error Occurred (Word 97SR2)

    Kevin,

    It looks like ths code would work in pretty much any VBA application as long as you had a reference set to the VBE. Have you tested it anywhere but Word?
    Charlotte

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

    Re: Procedure Name Where Error Occurred (Word 97SR2)

    Kevin,

    I had a couple of problems, so I haven't checked it out properly:

    .The group starting .txtErrorMsg gave a compile error. I changed them to <pre> "Error Number: " & CStr(Err.Number) & _
    " Description: " & Err.Description & vbNewLine & _ </pre>


    "Option explicit" gave me errors on vErrorMsg and vErrorSeverity

    I prefer explicit definitions- "strMsg as string" not "strMsg$"

    The line "Set oMod = VBE.ActiveVBProject.VBComponents(strMod).CodeModul e" gave me a "type mismatch" error.

    The routine "Call ScreenToClipboard" was not included.

    I could see where you are going, and it has promise- almost there.

    Unfortunately, I could not get it to work in a straight VB environment, even with the references set to VB Extensibility. That would be extremely useful if I could get it working there.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  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: Procedure Name Where Error Occurred (Word 97SR2)

    Thanks for taking the time to test this and give some feedback. Very much appreciated.

    The key piece is the GetSubName function which uses the .Find method to locate the error message in the module and gets the line number using .ProcOfLine.

    That's the only part that gets nominated for the "good idea of the day award". The rest is just my idea of a useful error message. Adapt as you might see fit.

    The vars starting with v are globals declared elsewhere. The $ declares were just an experiment to see if the code was more readable with shorter lines. I usually declare explicitly too.

    You really don't need the VBA extensibility library. Just declare oMod as an Object. You'll notice I prepared for that be using the numeric value 0 for vbext_pk_Proc.

    Just comment out ScreenToClipboard function call.
    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
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Procedure Name Where Error Occurred (Word 97SR2)

    I have not tested anywhere but Word 97. I will test with Word 2002.

    You don't really need the reference to the VBE library. Just declare oMod as an Object. I'm using it that way now.

    Thanks for posting back on this.
    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>

Posting Permissions

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