Bryco 发表于 2006-10-6 13:19:55

返回当前代码模块名称

本人'我们有一些现有的错误日志代码,当遇到错误时,它会将日期、时间和错误描述写入.LOG文件 我想添加的一件事(不需要硬编码)是触发错误消息的代码mod的名称
例如,如果CommandButton1\u Click事件中发生错误,我希望代码能够神奇地自动执行;“知道”;错误发生的位置 我假设我需要为VBA扩展性添加一个引用,但从那里我不知道该怎么做。

火影灌水 发表于 2006-10-6 14:35:28

听起来GetModuleFileName就是您需要的
API Guide(可从www.allapi.net免费获得)有一个示例,介绍了如何使用VB(a)中的GetModuleFileName和许多其他WIN32 API函数
AllAPI还提供了一个名为ApiViewer的便捷程序,供免费下载 它将为您创建函数、常量和类型声明,并将它们放在剪贴板上,这样您就可以将它们粘贴到应用程序中

菜籽客栈 发表于 2006-10-6 14:37:52

嗯;再想一想,我想我误解了你的问题。

赵阳 发表于 2006-10-6 15:16:14

嗯……是的……不完全是我的'我在找
此处'这是我现在在一些Access DB新闻组中找到的代码:
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

将以前的代码放入新的DVB时,将吐出以下内容:
ThisDrawing
Module1
    WhatProcs
    Test1
    Test2
我希望它只吐出WhatProcs,因为这是运行代码的过程&nbsp;如果我从Test1子系统中运行一些代码,我希望它返回Test1(天哪,我希望这是有意义的,因为我不确定我是否还能继续下去)
回顾过去,也许;模块;使用的术语是错误的&nbsp;可能应该&#039;ve使用了“;程序;??相反&nbsp;我不知道……我&#039;我筋疲力尽了&nbsp;Can#039;不要直接思考&nbsp;咖啡因没有帮助&nbsp;动机f-a-d-i-n-g fast。

教皇厅 发表于 2006-10-7 11:39:36

试试这个,它要求您对每个子节点都进行错误控制,这根本不是问题
Randall Rath编写了一个自动错误处理函数,可以为您编写行,非常好
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 在另一个模块中,我进行了测试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

宫非 发表于 2006-10-13 10:09:58

谢谢..我&#039;我有空的时候去看看。
页: [1]
查看完整版本: 返回当前代码模块名称