全部显示

Lines 属性

       

Lines 属性返回一个字符串,该字符串包含标准模块类模块中指定的一行或数行的内容。String 型,只读。

expression.Lines(Line, NumLines)

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

Line  必需 Long 型。返回第一行的行号。

NumLines  必需 Long 型。返回行数。

说明

Lines 属性仅在使用 Visual Basic 时才可用。

模块中的行从 1 开始编号。例如,读取 linenumlines 参数值都是 1 的 Lines 属性时,Lines 属性返回的字符串包含模块中第一行的文本。

要将一行文本插入模块,请使用 InsertLines 方法。

示例

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

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

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

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