技術(shù) 點
- 技術(shù)
- 點
- V幣
- 點
- 積分
- 9818
|
11#

樓主 |
發(fā)表于 2010-10-31 15:07:32
|
只看該作者
本帖最后由 tanhong 于 2010-10-31 15:56 編輯
6.11 代碼模塊中刪除代碼操作
6.11.1 刪除指定行代碼
- '過程功能:刪除指定行代碼
- '輸入?yún)?shù):VBCompName (String字符串變量) 指定模塊名
- ' StartLine 代碼起始行
- ' LinesNum 代碼行數(shù),默認(rèn)為一行
- Sub DelLinesCodes (VBCompName As String, StartLine As Long, _
- Optional LinesNum As Long = 1)
- Dim VBProj As VBProject
- Dim VBComps As VBComponents
-
- Set VBProj = VBE.ActiveVBProject
- Set VBComps = VBProj.VBComponents
-
- VBComps(VBCompName).CodeModule.DeleteLines StartLine, LinesNum
- End Sub
- '******************************************************************
- '調(diào)用示例一:刪除"模塊1"中,第一行代碼
- Call DelLinesCodes("模塊1", 1)
- '******************************************************************
- '調(diào)用示例二:刪除"模塊1"中,從第一行到第十行代碼
- CAll DelLinesCodes("模塊1", 1, 10)
復(fù)制代碼
6.11.2 刪除指定過程所有代碼
- '刪除指定過程代碼
- Public Sub DelProcCodes(VBCompName As String, VBProcName As String)
- Dim VBProj As VBProject
- Dim VBComps As VBComponents
- Dim ProcKind As vbext_ProcKind
-
- Set VBProj = VBE.ActiveVBProject
- Set VBComps = VBProj.VBComponents
-
- With VBComps (VBCompName).CodeModule
- .DeleteLines .ProcStartLine (VBProcName, ProcKind), _
- .ProcCountLines (VBProcName, ProcKind)
- End With
- End Sub
- '******************************************************************
- '調(diào)用示例:刪除“模塊1”中,“我的過程”所有代碼
- Call DelProcCodes ("模塊1", "我的過程")
復(fù)制代碼
6.11.3 刪除部件或模塊中所有代碼
- '刪除指定模塊中所有代碼
- Public Sub DelVBCompCodes (ByVal VBCompName As String)
- Dim VBProj As VBProject
- Dim VBComps As VBComponents
-
- '實例對象
- Set VBProj = VBE.ActiveVBProject
- Set VBComps = VBProj.VBComponents
- '從代碼模塊中第一行到最后一行執(zhí)行刪除
- VBComps(VBCompName).CodeModule.DeleteLines 1, _
- VBComps(VBCompName).CodeModule.CountOfLines
- End Sub
- '******************************************************************
- '調(diào)用示例:刪除“模塊1”中所有代碼
- Call DelVBCompCodes ("模塊1")
復(fù)制代碼
6.12 添加事件過程代碼操作
6.12.1 向指定部件對象添加事件
- '過程功能:創(chuàng)建一個事件過程
- '輸入?yún)?shù):VBCompNameOrIndex(Variant)部件名或索引
- ' strEventProc(String)事件程序
- ' strEventObj(String)事件對象
- ' strInsertCode(String)事件中欲插入代碼,默認(rèn)為空
- Sub CreateEventProcCode (VBCompNameOrIndex As Variant, _
- strEventProc As String, _
- strEventObj As String, _
- Optional strInsertCode As String = "")
- Dim VBProj As VBProject
- Dim VBComp As VBComponent
- Dim CodeMod As CodeModule
- Dim LineNum As Long
-
- '實例化對象
- Set VBProj = VBE.ActiveVBProject
- Set VBComp = VBProj.VBComponents (VBCompNameOrIndex)
- Set CodeMod = VBComp.CodeModule
-
- With CodeMod
- LineNum = .CreateEventProc (strEventProc, strEventObj)
-
- '是否為事件添了代碼,如未添加則退出
- If strInsertCode = vbNullString Then Exit Sub
- '從事件代碼之后插入新代碼
- LineNum = LineNum + 1
- .InsertLines LineNum, strInsertCode
- End With
- End Sub
- '************************************************
- '調(diào)用示例一:在窗體1中創(chuàng)建窗體加載事件,并加入代碼
- Dim strProcCode As String
- strProcCode = Space(4) & "Msgbox " & Chr(34) & "這是創(chuàng)建事件代碼演示!"
- Call CreateEventProcCode("Form_窗體1", "Load", "Form", strProcCode)
- '************************************************
- '調(diào)用示例二:在窗體1中創(chuàng)建窗體打開事件,但不加入代碼
- Call CreateEventProcCode("Form_窗體1", "Open", "Form")
復(fù)制代碼 |
|