全部显示

DeleteLines 方法

       

DeleteLines 方法从标准模块类模块中删除代码行。

expression.DeleteLines(StartLine, Count)

expression   必需。返回“Applies To”列表中的一个对象的表达式。

StartLine  必需 Long 型。Long 型值,指定要删除的起始行的行号。

Count  必需 Long 型。Long 型值,指定要删除的行数。

说明

模块中行的编号从 1 开始。若要确定模块中的行数,请使用 CountOfLines 属性。

若要以一行替换另一行,请使用 ReplaceLine 方法。

示例

下面的示例从模块中删除指定的行:

Function DeleteWholeLine(strModuleName, strText As String) _
     As Boolean
    Dim mdl As Module, lngNumLines As Long
    Dim lngSLine As Long, lngSCol As Long
    Dim lngELine As Long, lngECol As Long
    Dim strTemp As String
    
    On Error GoTo Error_DeleteWholeLine
    DoCmd.OpenModule strModuleName
    Set mdl = Modules(strModuleName)
    
    If mdl.Find(strText, lngSLine, lngSCol, lngELine, lngECol) Then
        lngNumLines = Abs(lngELine - lngSLine) + 1
        strTemp = LTrim$(mdl.Lines(lngSLine, lngNumLines))
        strTemp = RTrim$(strTemp)
        If strTemp = strText Then
            mdl.DeleteLines lngSLine, lngNumLines
        Else
            MsgBox "Line contains text in addition to '" _
                & strText & "'."
        End If
    Else
        MsgBox "Text '" & strText & "' not found."
    End If
    DeleteWholeLine = True
    
Exit_DeleteWholeLine:
    Exit Function
    
Error_DeleteWholeLine:
    MsgBox Err & " :" & Err.Description
    DeleteWholeLine = False
    Resume Exit_DeleteWholeLine
End Function

可以从以下过程中调用该函数,从而在模块“模块1”中搜索常量的声明语句并将其删除。

Sub DeletePiConst()
    If DeleteWholeLine("Module1", "Const conPi = 3.14") Then
        Debug.Print "Constant declaration deleted successfully."
    Else
        Debug.Print "Constant declaration not deleted."
    End If
End Sub