共用方式為


TextBox.SelText 屬性 (存取)

SelText 屬性會傳回包含所選的文字的字串。 讀取/寫入的 String

語法

表達。SelText

代表 TextBox 物件的變數。

註解

如果未不選取任何文字, SelText 屬性會包含 Null 值。

SelText 屬性會使用包含在控制項中選取之文字的字串運算式。 如果控制項包含選取的文字設定此屬性時,所選的文字會由新的 SelText 設定取代。

設定或傳回控制項的這個屬性,控制項必須具有焦點。 若要將焦點移到控制項、 使用 SetFocus 方法。

範例

下列範例會使用兩個事件程序來搜尋使用者指定的文字。 要搜尋的文字是在表單的 [載入事件] 程式中設定的。 使用者按一下以開始搜尋 (「尋找」按鈕的 Click 事件程序) 提示使用者輸入要搜尋的文字,並在搜尋成功時選取文字方塊中的文字。

Sub Form_Load() 
 Dim ctlTextToSearch As Control 
 Set ctlTextToSearch = Forms!Form1!TextBox1 
 ctlTextToSearch.SetFocus ' SetFocus to text box. 
 ctlTextToSearch.SelText = "This company places large orders " _ 
 & "twice a year for garlic, oregano, chilies and cumin." 
End Sub 
 
Sub Find_Click() 
 Dim strSearch As String, intWhere As Integer 
 Dim ctlTextToSearch As Control 
 ' Get search string from user. 
 With Me!Textbox1 
 strSearch = InputBox("Enter text to find:") 
 ' Find string in text. 
 intWhere = InStr(.Value, strSearch) 
 If intWhere Then 
 ' If found. 
 .SetFocus 
 .SelStart = intWhere - 1 
 .SelLength = Len(strSearch) 
 Else 
 ' Notify user. 
 MsgBox "String not found." 
 End If 
 End With 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應