|
8#
楼主 |
发表于 2010-10-31 15:06:07
|
只看该作者
6.5 获取过程代码行数信息
6.5.1指定过程总代码行数
- '函数功能:获得指定过程总的代码行数(含过程中的所有空行及注释)
- Public Function TotalCodeLinesInProc(CompsNameOrIndex, _
- strProcName As String, _
- Optional ProcKind As vbext_ProcKind = 0) As Long
- Dim VBProj As VBProject '工程对象
- Dim VBComp As VBComponent '部件对象
- Dim CodeMod As CodeModule '代码模块
-
- '设定为当前工程
- Set VBProj = VBE.ActiveVBProject
- '设定为指定部件
- Set VBComp = VBProj.VBComponents(CompsNameOrIndex)
- '设定为指定部件代码模块
- Set CodeMod = VBComp.CodeModule
- '过程计数输出
- TotalCodeLinesInProc = CodeMod.ProcCountLines(strProcName, ProcKind)
- End Function
- '******************************************************************
- '调用示例:获得部件"bas_ProcInfo"模块中,"ShowProcedureInfo"过程总行数
- Debug.Print TotalCodeLinesInProc("bas_ProcInfo", "ShowProcedureInfo")
复制代码
6.5.2 指定过程代码起始行数
- '函数功能:获得指定过程代码起始行(从过程之上的空行和注释计算)
- Public Function StartLineInProc (CompsNameOrIndex, _
- strProcName As String, _
- Optional ProcKind As vbext_ProcKind = 0) As Long
- Dim VBProj As VBProject
- Dim VBComp As VBComponent
- Dim CodeMod As CodeModule
-
- Set VBProj = VBE.ActiveVBProject
- Set VBComp = VBProj.VBComponents (CompsNameOrIndex)
- Set CodeMod = VBComp.CodeModule
-
- StartLineInProc = CodeMod.ProcStartLine(strProcName, ProcKind)
- End Function
- '******************************************************************
- '调用示例:获得部件"bas_ProcInfo"模块中,"ShowProcedureInfo"过程起始行号
- Debug.Print StartLineInProc ("bas_ProcInfo", "ShowProcedureInfo")
复制代码
6.5.3 指定过程实际代码起始行数
- '函数功能:获得过程第一行代码行(从过程的实际代码行计算,不含过程之上空行和注释)
- Public Function CodeBodyLineInProc (CompsNameOrIndex, _
- strProcName As String, _
- Optional ProcKind As vbext_ProcKind = 0) As Long
- Dim VBProj As VBProject
- Dim VBComp As VBComponent
- Dim CodeMod As CodeModule
-
- Set VBProj = VBE.ActiveVBProject
- Set VBComp = VBProj.VBComponents(CompsNameOrIndex)
- Set CodeMod = VBComp.CodeModule
-
- CodeBodyLineInProc = CodeMod.ProcBodyLine(strProcName, ProcKind)
- End Function
- '******************************************************************
- '调用示例:获得部件"bas_ProcInfo"模块中,"ShowProcedureInfo"过程实际起始行号
- Debug.Print CodeBodyLineInProc ("bas_ProcInfo", "ShowProcedureInfo")
复制代码
6.5.4 指定过程实际代码行数
- '函数功能:获得指定过程实际代码行数(不包含空行和注释行)
- Public Function CodeLinesInProc(ByVal CompsNameOrIndex, _
- ByVal strProcName As String, _
- Optional ProcKind As vbext_ProcKind = 0) As Long
- Dim VBProj As VBProject
- Dim VBComp As VBComponent
- Dim CodeMod As CodeModule
-
- Dim ProcStart As Long '代码起始行
- Dim ProcTotal As Long '代码总行数
- Dim I As Integer '循环变量
- Dim strCode As String '代码
- Dim LineCount As Long '行计数变量
- 实例化对象
- Set VBProj = VBE.ActiveVBProject
- Set VBComp = VBProj.VBComponents(CompsNameOrIndex)
- Set CodeMod = VBComp.CodeModule
- '获取开始行号和总行数
- ProcStart = CodeMod.ProcStartLine (strProcName, ProcKind)
- ProcTotal = CodeMod.ProcCountLines(strProcName, ProcKind) + ProcStart
-
- For I = ProcStart To ProcTotal
- '将代码赋值给字符串变量
- strCode = CodeMod.Lines(I, 1)
- '跳过空行和注释行
- If Trim (strCode) = vbNullString Or Left (Trim (strCode), 1) = Chr (39) Then
- Else
- LineCount = LineCount + 1
- End If
- Next I
- '实际行数输出
- CodeLinesInProc = LineCount
- End Function
- '******************************************************************
- '调用示例:获得部件"bas_ProcInfo"模块中,"ShowProcedureInfo"过程实际行数
- Debug.Print CodeLinesInProc ("bas_ProcInfo", "ShowProcedureInfo")
复制代码 |
|