Showing results for 
Search instead for 
Did you mean: 

C# Script SAP GUI Select text and hit copy button



I am trying to select the text on a SAP GUI control and copy it to the clipboard.

I have been able to navigate through the tabs and menus but cannot understand how the text can be selected (can be small or longer texts) and the button can be pressed to achieve this.

Image for reference attached.

We have been blocked for using the scripts recording and if it wasn't for Stefan Schnell tracker it would be even worse. We can still run scripts to the moment and it is a very useful tool.

Can anyone help?

Thank you.


View Entire Topic
Active Contributor

Hello @ptuga,

try this to copy the text of a TextEdit control to the clipboard. This C# example selects the first 1024 characters of TextEdit control, gets the selected text and copies it to the clipboard.


ID = InvokeMethod(session, "findById", new object[1]{"wnd[0]/usr/shell"});
InvokeMethod(ID, "SetSelectionIndexes", new object[2]{0, 1024});
string selectedText = GetProperty(ID, "SelectedText", new object[0]);


Important hint: To use the clipboard SetText method from System.Windows.Forms it is necessary to set the  ApartmentStateAttribute of the main function to single-threaded apartment (STA) [STAThread].

Best regards

0 Kudos

@stefan_schnell: Thank you for the hint on how to select the text on such object. If we don't know how many chars we have, is 1024 the max? 

I have also used the GetProperty(editBoxID, "text") to get the contents without having to have any text selected.

I have tried both solutions and both work.