InsertText 方法在標準模塊或類模塊中插入一個指定的文本字符串。
expression.InsertText(Text)
expression 必需。返回以上對象之一的表達式。
Text 必需 String 型。要插入到模塊中的文本。
應用于 Application 對象的 InsertText 方法。
InsertText 方法將指定的文本字符串插入到應用程序中。
expression.InsertText(Text, ModuleName)
expression 必需。返回以上對象之一的表達式。
Text 必需 String 型。要插入到模塊中的文本。
ModuleName 必需 String 型。應用程序的模塊名。
當使用 InsertText 方法來插入字符串時,Microsoft Access 將把新文本放在模塊尾部,所有其他過程之后。
若要一次添加多行,請在組成 text 參數(shù)的字符串中需要分行的位置包含固有常量 vbCrLf。該常量將強制換行。
若要指定在某行插入文本,請使用 InsertLines 方法。如果要在模塊的聲明節(jié)插入過程代碼,請使用 InsertLines 方法,不要用 InsertText 方法。
注釋 在 Microsoft Access 的舊版本中,InsertText 方法是 Application 對象的方法。雖然仍可以使用 Application 對象的 InsertText 方法,但是建議改用 Module 對象的 InsertText 方法。
下面的示例在標準模塊中插入文本字符串:
Function InsertProc(strModuleName) As Boolean
Dim mdl As Module, strText As String
On Error GoTo Error_InsertProc
' Open module.
DoCmd.OpenModule strModuleName
' Return reference to Module object.
Set mdl = Modules(strModuleName)
' Initialize string variable.
strText = "Sub DisplayMessage()" & vbCrLf _
& vbTab & "MsgBox ""Wild!""" & vbCrLf _
& "End Sub"
' Insert text into module.
mdl.InsertText strText
InsertProc = True
Exit_InsertProc:
Exit Function
Error_InsertProc:
MsgBox Err & ": " & Err.Description
InsertProc = False
Resume Exit_InsertProc
End Function