Author Topic: Return current code module name  (Read 2221 times)

0 Members and 1 Guest are viewing this topic.

Guest

  • Guest
Return current code module name
« on: October 06, 2006, 01:19:55 PM »
I've got some existing error logging code that writes to a .LOG file the date, time and error description when an error is encountered.  The one thing I would like to add (without hardcoding it) is the name of the code mod that triggerered the error message.

For example, if an error occurs within the CommandButton1_Click event, I would like the code to auto-magically "know" where the error occurred.  I assume I need to add a reference for the VBA Extensibility, but from there I have NO IDEA which way to go.

Chuck Gabriel

  • Guest
Re: Return current code module name
« Reply #1 on: October 06, 2006, 02:35:28 PM »
It sounds like GetModuleFileName is what you need.

API-Guide (available free from www.allapi.net) has an example of how to use GetModuleFileName and a lot of other WIN32 API functions from VB(A).

AllAPI also offers a handy program called ApiViewer for free download.  It will create your function, constant and type declarations for you and put them on your clipboard, so you can just paste them into your app.
« Last Edit: October 06, 2006, 02:36:47 PM by Chuck Gabriel »

Chuck Gabriel

  • Guest
Re: Return current code module name
« Reply #2 on: October 06, 2006, 02:37:52 PM »
Hmmm.  On second thought, I think I misunderstood your question.

Guest

  • Guest
Re: Return current code module name
« Reply #3 on: October 06, 2006, 03:16:14 PM »
Mmmmm.... yeah.... not EXACTLY what I'm looking for.

Here's the code that I have right now that I found on some Access DB newsgroup:

Code: [Select]
Sub WhatProcs()
    Dim oProj As VBProject
    Dim oComp As VBComponent
    Dim oMod As CodeModule
    Dim nLine As Long
    Dim strProcName As String
   
    Set oProj = ThisDrawing.Application.VBE.ActiveVBProject
   
    For Each oComp In oProj.VBComponents
        ' Confusingly, each oComp corresponds to a "Module"
        ' as shown in the Project Explorer. Then oMod
        ' represents the code itself, which has no name.
        Set oMod = oComp.CodeModule
        Debug.Print oComp.Name
       
        ' Step past the declarations at the top
        nLine = oMod.CountOfDeclarationLines + 1
       
        Do While nLine < oMod.CountOfLines
            ' Get the name of the procedure enclosing the line
            strProcName = oMod.ProcOfLine(nLine, vbext_pk_Proc)
            Debug.Print vbTab & strProcName
           
            ' Add this procedure's line count to the current line
            ' to find the next procedure (or the end of the module)
            nLine = nLine + oMod.ProcCountLines(strProcName, vbext_pk_Proc)
        Loop
    Next oComp
   
    Set oMod = Nothing
    Set oComp = Nothing
    Set oProj = Nothing
End Sub

Public Sub Test1()
    MsgBox "This is a test"
End Sub

Public Sub Test2()
    MsgBox "This is also test"
End Sub


The previous code when put into a new DVB, will spit out the following:

Code: [Select]
ThisDrawing
Module1
    WhatProcs
    Test1
    Test2

I would like it spit out only WhatProcs because that is the procedure which is running the code.  If I ran some code from the Test1 sub, I would like it to return Test1 (God, I hope this is making sense becasue I'm not sure if I can follow this anymore).


Looking back, maybe "module" was the wrong term to use.  Probably should've used "procedure"?? instead??  I dunno... I'm burnt out.  Can't think straight.  Caffeine not helping.  Motivation f-a-d-i-n-g fast.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Return current code module name
« Reply #4 on: October 07, 2006, 11:39:36 AM »
Try this, it requires you to have error control on every sub, which is not a problem at all.
Randall Rath wrote an automatic error handler function that writes the lines for you, very nice.
Code: [Select]
Sub ActiveCode()

    Dim oIDE As VBE
    Dim oPane As CodePane
    Dim oMod As CodeModule
    Dim sProc As String
    Dim lngSC As Long 'Start column
    Dim lngEC As Long 'End Column
    Dim lngSL As Long 'Start Line
    Dim lngEL As Long 'End Line
    Dim sActiveProject As String
    Dim sActModule As String

    Set oIDE = Application.VBE
    sActiveProject = oIDE.ActiveVBProject.Name
    Set oPane = oIDE.ActiveCodePane
    Set oMod = oPane.CodeModule
    sActModule = oPane.CodeModule.Name
    oPane.GetSelection lngSL, lngSC, lngEL, lngEC
    sProc = oMod.ProcOfLine(lngSL, vbext_pk_Proc)
    Debug.Print sActiveProject, sActModule, sProc
    Debug.Print Err.Number, Err.Description
   
End Sub

And in another module I put the test
Code: [Select]
Sub errTest()
    On Error GoTo Err_Control
    Dim v As Integer
    v = 1E+43
    Debug.Print v
Exit_Here:
    Exit Sub
Err_Control:
    Select Case Err.Number
    'Add your Case selections here
        Case Else
       
        ActiveCode
        Err.Clear
        Resume Exit_Here
    End Select
End Sub

Guest

  • Guest
Re: Return current code module name
« Reply #5 on: October 13, 2006, 10:09:58 AM »
Thanks.. I'll check it out when I get a few spare minutes.