Copying and Pasting Text in an Edit Control

Applies to TestComplete 14.50, last modified on April 22, 2021

While testing edit controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

You can modify text within an edit control by entering text, but in some cases, it may be more convenient to paste text from the clipboard to an edit control. You may also need to copy text from an edit control, for example, if you need to paste it to another control. There are several ways to copy text from or paste it to an edit control.

Copying Text

If you want to copy text to the clipboard, you need to select the needed text before copying it (see Selecting Text Within an Edit Control). You can select some part of the text or the whole text within the control. After that, you can copy it in the following ways:

  • You can copy text by using the Ctrl+C (Ctrl+Ins) shortcut. For more information on simulating shortcuts, see the Simulating Keystrokes topic. Below is an example that demonstrates how to copy text by simulating keystrokes:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("Do cats eat bats?");

      // Select text
      Edit.Keys("^a");

      // Copy text
      Edit.Keys("^c");

      // Post the clipboard contents to the log
      Log.Message(Sys.Clipboard);
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("Do cats eat bats?")
    
      # Select text 
      Edit.Keys("^a")
    
      # Copy text
      Edit.Keys("^c")
    
      # Post the clipboard contents to the log
      Log.Message(Sys.Clipboard)

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      WshShell.Run "notepad.exe", SW_SHOWNORMAL

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "Do cats eat bats?"

      ' Select text
      Edit.Keys "^a"

      ' Copy text
      Edit.Keys "^c"

      ' Post the clipboard contents to the log
      Log.Message Sys.Clipboard
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('Do cats eat bats?');

      // Select text
      Edit.Keys('^a');

      // Copy text
      Edit.Keys('^c');

      // Post the clipboard contents to the log
      Log.Message(Sys.Clipboard);
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("Do cats eat bats?");

      // Select text
      Edit["Keys"]("^a");

      // Copy text
      Edit["Keys"]("^c");

      // Post the clipboard contents to the log
      Log["Message"](Sys["Clipboard"]);
     }

  • You can copy text by using the Copy item of the edit control’s context menu. To copy text, perform the following:

    The following code demonstrates how you can copy text using the context menu:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("Do cats eat bats?");

      // Select text
      Edit.Keys("^a");

      // Copy the text via the context menu using the PopupMenu.Click method
      Edit.ClickR();
      Edit.PopupMenu.Click("Copy");

      // Post the clipboard content to the log
      Log.Message(Sys.Clipboard);

      // Change the text
      Edit.Keys("Do bats eat cats?");
      Edit.Keys("^a");

      // Copy the text via the context menu using the hot key
      Edit.Keys("[Apps]");
      Edit.Keys("c");

      // Post the clipboard content to the log
      Log.Message(Sys.Clipboard);
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("Do cats eat bats?")
    
      # Select text 
      Edit.Keys("^a")
    
      # Copy the text via the context menu using the PopupMenu.Click method
      Edit.ClickR()
      Edit.PopupMenu.Click("Copy")
    
      # Post the clipboard content to the log
      Log.Message(Sys.Clipboard)
    
      # Change the text
      Edit.Keys("Do bats eat cats?")
      Edit.Keys("^a")
    
      # Copy the text via the context menu using the hot key
      Edit.Keys("[Apps]")
      Edit.Keys("c")
    
      # Post the clipboard content to the log
      Log.Message(Sys.Clipboard)

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      WshShell.Run "notepad.exe", SW_SHOWNORMAL

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "Do cats eat bats?"

      ' Select text
      Edit.Keys "^a"

      ' Copy the text via the context menu using the PopupMenu.Click method
      Edit.ClickR
      Edit.PopupMenu.Click "Copy"

      ' Post the clipboard content to the log
      Log.Message Sys.Clipboard

      ' Change the text
      Edit.Keys "Do bats eat cats?"
      Edit.Keys "^a"

      ' Copy the text via the context menu using the hot key
      Edit.Keys "[Apps]"
      Edit.Keys "c"

      ' Post the clipboard content to the log
      Log.Message Sys.Clipboard
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
     Edit.Keys('Do cats eat bats?');

      // Select text
      Edit.Keys('^a');

      // Copy the text via the context menu using the PopupMenu.Click method
      Edit.ClickR();
      Edit.PopupMenu.Click('Copy');

      // Post the clipboard content to the log
      Log.Message(Sys.Clipboard);

      // Change the text
      Edit.Keys('Do bats eat cats?');
      Edit.Keys('^a');

      // Copy the text via the context menu using the hot key
      Edit.Keys('[Apps]');
      Edit.Keys('c');

      // Post the clipboard content to the log
      Log.Message(Sys.Clipboard);
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("Do cats eat bats?");

      //Select text
      Edit["Keys"]("^a");

      // Copy the text via the context menu using the PopupMenu.Click method
      Edit["ClickR"]();
      Edit["PopupMenu"]["Click"]("Copy");

      // Post the clipboard content to the log
      Log.Message(Sys.Clipboard);

      // Change the text
      Edit["Keys"]("Do bats eat cats?");
      Edit["Keys"]("^a");

      // Copy the text via the context menu using the hot key
      Edit["Keys"]("[Apps]");
      Edit["Keys"]("c");

      // Post the clipboard content to the log
      Log.Message(Sys.Clipboard);
     }

Pasting Text

When working with an edit control, you may need to paste text from the clipboard to this control. You should specify the insertion point (see Setting Insertion Point in an Edit Control) to add the clipboard content, or select any portion of text to replace it with the clipboard content (see also Replacing Text in an Edit Control). You can paste text to an edit control in the following ways:

  • You can paste text by using the Ctrl+V (Shift+Ins) shortcut. Simulation of the Ctrl+V shortcut can be done via the Keys action. Below is an example that demonstrates how to paste text by simulating keystrokes:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("Do cats eat bats?");

      // Copy text
      Edit.Keys("[Left]![Left]![Left]![Left]![Left]");
      Edit.Keys("^c[Right]");

      // Paste the copied text to the edit control
      Edit.Keys("[Right][Right][Right][Right][Enter]");
      Edit.Keys("^v");
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("Do cats eat bats?")
    
      # Copy text 
      Edit.Keys("[Left]![Left]![Left]![Left]![Left]")
      Edit.Keys("^c[Right]")
    
      # Paste the copied text to the edit control
      Edit.Keys("[Right][Right][Right][Right][Enter]")
      Edit.Keys("^v")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      WshShell.Run "notepad.exe", SW_SHOWNORMAL

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "Do cats eat bats?"

      ' Copy text
      Edit.Keys "[Left]![Left]![Left]![Left]![Left]"
      Edit.Keys "^c[Right]"

      ' Paste the copied text to the edit control
      Edit.Keys "[Right][Right][Right][Right][Enter]"
      Edit.Keys "^v"
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('Do cats eat bats?');

      // Copy text
      Edit.Keys('[Left]![Left]![Left]![Left]![Left]');
      Edit.Keys('^c[Right]');

      // Paste the copied text to the edit control
      Edit.Keys('[Right][Right][Right][Right][Enter]');
      Edit.Keys('^v');
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("Do cats eat bats?");

      // Copy text
      Edit["Keys"]("[Left]![Left]![Left]![Left]![Left]");
      Edit["Keys"]("^c[Right]");

      // Paste the copied text to the edit control
      Edit["Keys"]("[Right][Right][Right][Right][Enter]");
      Edit["Keys"]("^v");
     }

  • You can paste text by using the context menu. You can call this menu, for example, via the ClickR action. After that, you should select the Paste command. To do this, use the Window.PopupMenu.Click method or simulate the P key press, which is the hot key of this command. The following code demonstrates how you can paste text using the context menu:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("Do bats eat cats?");

      // Copy the desired text
      Edit.Keys("[Left]![Left]![Left]![Left]![Left]");
      Edit.Keys("^c");

      // Paste the text via the context menu using the PopupMenu.Click method
      Edit.Keys("[Right][Right][Right][Right][Right][Enter]");
      Edit.ClickR();
      Edit.PopupMenu.Click("Paste");

      // Paste the text via the context menu using the hot key
      Edit.Keys("[Enter]");
      Edit.Keys("[Apps]");
      Edit.Keys("p");
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("Do bats eat cats?")
    
      # Copy the desired text 
      Edit.Keys("[Left]![Left]![Left]![Left]![Left]")
      Edit.Keys("^c")
    
      # Paste the text via the context menu using the PopupMenu.Click method
      Edit.Keys("[Right][Right][Right][Right][Right][Enter]")
      Edit.ClickR()
      Edit.PopupMenu.Click("Paste")
    
      # Paste the text via the context menu using the hot key
      Edit.Keys("[Enter]")
      Edit.Keys("[Apps]")
      Edit.Keys("p")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      WshShell.Run "notepad.exe", SW_SHOWNORMAL

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "Do bats eat cats?"

      ' Copy the desired text
      Edit.Keys "[Left]![Left]![Left]![Left]![Left]"
      Edit.Keys "^c"

      ' Paste the text via the context menu using the PopupMenu.Click method
      Edit.Keys "[Right][Right][Right][Right][Right][Enter]"
      Edit.ClickR
      Edit.PopupMenu.Click "Paste"

      ' Paste the text via the context menu using the hot key
      Edit.Keys "[Enter]"
      Edit.Keys "[Apps]"
      Edit.Keys "p"
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('Do bats eat cats?');

      // Copy the desired text
      Edit.Keys('[Left]![Left]![Left]![Left]![Left]');
      Edit.Keys('^c');

      // Paste the text via the context menu using the PopupMenu.Click method
      Edit.Keys('[Right][Right][Right][Right][Right][Enter]');
      Edit.ClickR();
      Edit.PopupMenu.Click('Paste');

      // Paste the text via the context menu using the hot key
      Edit.Keys('[Enter]');
      Edit.Keys('[Apps]');
      Edit.Keys('p');
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad").Window("Edit");
      Edit["Keys"]("Do bats eat cats?");

      // Copy the desired text
      Edit["Keys"]("[Left]![Left]![Left]![Left]![Left]");
      Edit["Keys"]("^c");

      // Paste the text via the context menu using the PopupMenu.Click method
      Edit["Keys"]("[Right][Right][Right][Right][Right][Enter]");
      Edit["ClickR"]();
      Edit["PopupMenu"]["Click"]("Paste");

      // Paste the text via the context menu using the hot key
      Edit["Keys"]("[Enter]");
      Edit["Keys"]("[Apps]");
      Edit["Keys"]("p");
     }

Using the Sys.Clipboard Property

This property belongs to the Sys object and allows you to put data to and retrieve it from the clipboard. You can copy and paste data by doing any of the following:

  • You can obtain the required text by using the wSelection or wText property and put it to the clipboard via the Sys.Clipboard property. Using the wSelection property allows you to obtain the selected text, and if you use the wText property, you can obtain the whole text the edit control contains. The example below demonstrates how you can copy text to the clipboard using these properties:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("Clipboard content");

      // Put the whole text to the clipboard
      Sys.Clipboard = Edit.wText;
      Log.Message(Sys.Clipboard)

      // Put the selected text to clipboard
      Edit.Keys("![Left]![Left]![Left]![Left]![Left]![Left]![Left]");
      Sys.Clipboard = Edit.wSelection;
      Log.Message(Sys.Clipboard);
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("Clipboard content")
    
      # Put the whole text to the clipboard
      Sys.Clipboard = Edit.wText
      Log.Message(Sys.Clipboard)
    
      # Put the selected text to clipboard
      Edit.Keys("![Left]![Left]![Left]![Left]![Left]![Left]![Left]")
      Sys.Clipboard = Edit.wSelection
      Log.Message(Sys.Clipboard)

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      WshShell.Run "notepad.exe", SW_SHOWNORMAL

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "Clipboard content"

      ' Put the whole text to the clipboard
      Sys.Clipboard = Edit.wText
      Log.Message Sys.Clipboard

      ' Put the selected text to the clipboard
      Edit.Keys "![Left]![Left]![Left]![Left]![Left]![Left]![Left]"
      Sys.Clipboard = Edit.wSelection
      Log.Message Sys.Clipboard
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('Clipboard content');

      // Put the whole text to the clipboard
      Sys.Clipboard := Edit.wText;
      Log.Message(Sys.Clipboard);

      // Put the selected text to the clipboard
      Edit.Keys('![Left]![Left]![Left]![Left]![Left]![Left]![Left]');
      Sys.Clipboard := Edit.wSelection;
      Log.Message(Sys.Clipboard);
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("Clipboard content");

      // Put the whole text to the clipboard
      Sys["Clipboard"] = Edit["wText"];
      Log["Message"](Sys["Clipboard"]);

      // Put the selected text to the clipboard
      Edit["Keys"]("![Left]![Left]![Left]![Left]![Left]![Left]![Left]");
      Sys["Clipboard"] = Edit["wSelection"];
      Log["Message"](Sys["Clipboard"]);
     }

  • You can copy the desired text to the clipboard by using any of the above-mentioned methods and then replace the control’s text with the clipboard contents by using the SetText action or the wText property.

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("The text to be replaced");
      Sys.Clipboard = "New text";

      // Replace all text within the Edit control
      Edit.SetText(Sys.Clipboard);
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("The text to be replaced")
      Sys.Clipboard = "New text"
    
      # Replace all text within the Edit control
      Edit.SetText(Sys.Clipboard)

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      WshShell.Run "notepad.exe", SW_SHOWNORMAL

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "The text to be replaced"
      Sys.Clipboard = "New text"

      ' Replace all text within the Edit control
      Edit.SetText Sys.Clipboard
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('The text to be replaced');
      Sys.Clipboard := 'New text';

      // Replace all text within the Edit control
      Edit.SetText(Sys.Clipboard);
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("The text to be replaced");
      Sys["Clipboard"] = "New text";

      // Replace all text within the Edit control
      Edit["SetText"](Sys["Clipboard"]);
     }

See Also

Working With Edit Controls
Simulating Menu Actions
Simulating Keystrokes
Entering Text into an Edit Control
Selecting Text Within an Edit Control
Setting Insertion Point in an Edit Control
wSelection Property (Edit Controls)
wText Property (Edit Controls)
SetText Action (Edit and Combo Box Controls)

Highlight search results