GetText Method Example

This example uses the GetText method to copy a text string from the Clipboard object to a string variable. To try this example, paste the code into the Declarations section of a form with a TextBox control named Text1, and then press F5 and click the form.

Private Sub Form_Click ()
   Dim I, Msg, Temp   ' Declare variables.
   On Error Resume Next   ' Set up error handling.
   Msg = "Type anything you like into the text box below."
   Text1.Text = InputBox(Msg)  ' Get text from user.
   Msg = "Choose OK to copy the contents of the text box "
   Msg = Msg & "to the Clipboard."
   MsgBox Msg   ' Display message.
   Clipboard.Clear   ' Clear Clipboard.
   Clipboard.SetText Text1.Text  ' Put text on Clipboard.
   If Clipboard.GetFormat(vbCFText) Then
      Text1.Text = ""   ' Clear the text box.
      Msg = "The text is now on the Clipboard. Choose OK "
      Msg = Msg & "to copy the text from the Clipboard back "
      Msg = Msg & "to the text box."
      MsgBox Msg   ' Display message.
      Temp = Clipboard.GetText(vbCFText)   ' Get Clipboard text.
      For I = Len(Temp) To 1 Step -1   ' Reverse the text.
         Text1.Text = Text1.Text & Mid(Temp, I, 1)   
      Next I
   Else
      Msg = "There is no text on the Clipboard."
      MsgBox Msg   ' Display error message.
   End If
End Sub