Code Module Methods

Name Returns Parameters Description

AddFromFile FileName As String Reads code from a text file and adds it to the end of the code module. It does not check if the names of procedures read from a file already exist in the module.

AddFromString String As String Adds code from a string to the end of the code module.

CreateEvent Long EventName As String, Creates an empty event procedure

Proc ObjectName As String in a module, filling in the event parameters for you. Cannot be used on standard modules, as they do not support events. The ObjectName must be a valid object for the class module, and the EventName must be a valid event for that object.

Name

Returns

Parameters

Description

DeleteLines

StartLine As Long, Count As Long

Deletes lines from a code module, starting at StartLine, for Count lines.

Find

Boolean

Target As String, StartLine As Long, StartColumn As Long, EndLine As Long, EndColumn As Long, WholeWordAs Boolean, MatchCase As Boolean, PatternSearch As Boolean

Locates a string within a code module, or section of a code module. It provides the same functionality as the VBE's Find dialog.

InsertLines

Line As Long, String As String

Adds code from a string into the middle of a code module, inserting the code before the Line given.

ReplaceLines

Line As Long, String As String

Adds code from a string into the middle of a code module, replacing the Line given.

There are a number of CodeModule examples in Chapter 26. The following example identifies the procedure for a given line and displays its type, name, and line count:

Sub WhichProc()

Dim lLine As Long, iProcKind As Long, lLineCount As Long Dim sProc As String, sMsg As String Dim oActiveCM As VBIDE.CodeModule lLine = CLng(InputBox("Which line?"))

'Cancelled?

If lLine = 0 Then Exit Sub

'Get the currently active code module

Set oActiveCM = Application.VBE.ActiveCodePane.CodeModule

'Get the name and type of the procedure at

'that line - iProcKind is filled in sProc = oActiveCM.ProcOfLine(lLine, iProcKind)

'We didn't get a name, so you must be in the Declarations section sMsg = "You are in the Declarations section" lLineCount = oActiveCM.CountOfDeclarationLines Else sMsg = "You are in "

'Display the type of the procedure...

Select Case iProcKind Case vbext_pk_Proc sMsg = sMsg & "Sub or Function procedure" Case vbext_pk_Get sMsg = sMsg & "Property Get procedure" Case vbext_pk_Let sMsg = sMsg & "Property Let procedure" Case vbext_pk_Set sMsg = sMsg & "Property Set procedure"

End Select

sMsg = sMsg & " '" & sProc & "'"

lLineCount = oActiveCM.ProcCountLines(sProc, iProcKind) End If

'Display the message

MsgBox sMsg & vbCrLf & "which has " & lLineCount & " lines."

End Sub

+1 0

Post a comment