Jump to content

Processing selected text, then replacing the selected text with the processed text


Go to solution Solved by IlanMS,

Recommended Posts

General purpose: The selected window has selected text. The script will process the text, and then replace the selected text with the processed text.

Current purpose: Get the selected text from the selected window.

I found 3 answers in the forum, each with it's problems:
Solution A:
 Basically copy to clipboard. Problems: does not work in all cases (Ctrl-C does not allways work. OP managed to improve this but not to 100% success). Also goes through the clipboard instead of directly read the text into a string variable.
Solution B:
 Use AutoIt built-in ControlCommand. Problem: Requires to know the control in which the selected test is. I couldn't find how to do this.
Solution C :
 
Use some VB code found in http://support.microsoft.com/kb/163434. Problems: I don't know how to incorporate VB code and how take the variable oText from the VB sub to my script:

So now I have to do 1. decide which solution to use. 2. Understand how to use it.

Here follows the solutions:
A:

 

Send ("{CTRLDOWN}")
        ;Try various methods until we get text on the clipboard
        Switch $rand
            Case 1
                Send("{INS}") 
            Case 2
                ControlSend($window_title, "", "", "{INS}")
            Case 3
                Send("c")
            Case 4
                ControlSend($window_title, "", "", "c^")
        EndSwitch


 B: (I know how to get hWnd):
<ControlCommand ( "hWnd", "My string variable?", Have no idea how to get this, "GetSelected", "My string variable here?">

C :
 

Sub GetSelectedText()

      On Error Resume Next
      Err.Clear

      Dim oText As TextRange

      ' Get an object reference to the selected text range.
      Set oText = Activewindow.Selection.TextRange

      ' Check to see whether error occurred when getting text object
      ' reference.
      If Err.Number <> 0 Then

         MsgBox "Invalid Selection. Please highlight some text " _
            & "or select a text frame and run the macro again.", _
            vbExclamation
         End

      End If

      ' Display the selected text in a message box.
      If oText.Text = "" Then
         MsgBox "No Text Selected.", vbInformation
      Else
         MsgBox oText.Text, vbInformation
      End If

   End Sub

Which solution to use, and how?
 

Edited by IlanMS
correction
Link to post
Share on other sites

First problem: The MS site you linked to does not exist (returned 404)  ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

Second problem, you did not specify the source of the selection. (Web page ?  Word document ?  3rd party Application ?  Else ?  All of the above ?)

Link to post
Share on other sites
5 minutes ago, Nine said:

Second problem, you did not specify the source of the selection. (Web page ?  Word document ?  3rd party Application ?  Else ?  All of the above ?)

Any source. Could be MS Word, form in browser, text in url line, notepad, or anything else.

Link to post
Share on other sites
16 minutes ago, water said:

First problem: The MS site you linked to does not exist (returned 404)  ;)

Can't help with that if MS removed it. However the code is in my message (option C).

Link to post
Share on other sites

Are all those sources in an available window (non-hidden, non-minimized) ? If available, is it an ACTIVE window ?  Can you create a replicate of the problem ?  Your AutoIt code is not really optimal BTW.

Link to post
Share on other sites
12 minutes ago, Nine said:

Are all those sources in an available window (non-hidden, non-minimized) ? If available, is it an ACTIVE window ?  Can you create a replicate of the problem ?  Your AutoIt code is not really optimal BTW.

Always active window (can get its handle with $hWnd = WinGetHandle("[active]"))
I can't help with "not optimal". I am a noob. I do my best to get answers by searching the forum, but naturally I can't understand most of the scripts and I try to treat them as encapsulated black boxes.

Link to post
Share on other sites

Code in option C is MS application related (Word, PowerPoint ...).

Did you search the forum for your problem? IIRC this has been discussed before. Can't remember if there was a solution or just a "can't be done" 🤔

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites
Posted (edited)
9 minutes ago, water said:

Code in option C is MS application related (Word, PowerPoint ...).

Did you search the forum for your problem? IIRC this has been discussed before. Can't remember if there was a solution or just a "can't be done" 🤔

I did search (wrote so in this topic opening message) and put here everything I thought relevant. At least option A is viable but not a total solution. As for B I honestly don't know (hoped to get answers here), and your answer here renders option C as useless.

At least we narrowed it down to two options, A which is less than a total solution (but I will use if I can't find anything better) and B which I have no idea how to use.

Edited by IlanMS
Link to post
Share on other sites

Try this :

#include <Constants.au3>
Sleep(5000) ; SELECT THE WINDOW YOU WANT TO COPY THE SELECTED TEXT FROM

ClipPut("")

Local $sTitle = WinGetTitle("[ACTIVE]")
If Not $sTitle Then Exit MsgBox($MB_SYSTEMMODAL, "", "No Window Selected")
ConsoleWrite($sTitle & @CRLF)

Send("^c")

Local $sText = ClipGet()
If Not $sText Then Exit MsgBox($MB_SYSTEMMODAL, "", "No text selected")
ConsoleWrite($sText & @CRLF)

If you find a situation where there is selected text and it is not working then tell us what is the source application ...

Edited by Nine
Link to post
Share on other sites
6 minutes ago, Nine said:

Try this :

#include <Constants.au3>
Sleep(5000) ; SELECT THE WINDOW YOU WANT TO COPY THE SELECTED TEXT FROM

ClipPut("")

Local $sTitle = WinGetTitle("[ACTIVE]")
If Not $sTitle Then Exit MsgBox($MB_SYSTEMMODAL, "", "No Window Selected")
ConsoleWrite($sTitle & @CRLF)

Send("^c")

Local $sText = ClipGet()
If Not $sText Then Exit MsgBox($MB_SYSTEMMODAL, "", "No text selected")
ConsoleWrite($sText & @CRLF)

If you find a situation where there is selected text and it is not working then tell us what is the source application ...

What should be the content of Constants.au3?

Link to post
Share on other sites

BTW:

When you want to answer, please do not quote the post. Simply add your reply at the end of the thread.
As we know what we have posted quotes are rarely needed. They just clutter the thread and make it hard to read.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

Look at the very end of the thread there is a reply box with you avatar on the left.  Just click the box...

Link to post
Share on other sites

 

23 hours ago, IlanMS said:

Any source. Could be MS Word, form in browser, text in url line, notepad, or anything else.

I've had to do this many times in a variety of programs and windows, and I have discovered that some windows are simply resistant to copying.

I have settled on just using the clipboard for this (you can try to save the existing clipboard to a variable and then restore it later).  The keyboard shortcut for copying text isn't the same in all programs, but as you may know, Ctrl+C works 90% of the time.

As for AutoIt's various Control functions, they only work in some windows.  Java programs, for example, often have no "controls" that AutoIt can detect.

Also, some programs require you to make the text pane active somehow, e.g. by clicking inside it.   In some programs, if the user had selected text but then moved away from the window (so that it is no longer the active window) and returns to the window again (so that it is the active window again), pressing Ctrl+C will not manage to copy the selected text until you have put the focus back inside the pane where the text is selected.  (And it's a bit of a catch-22, because as soon as you click inside that pane, the text gets deselected.)  But sometimes one can get the focus back to such a pane by e.g. right-clicking in the pane (and cancelling the right-click menu), or by pressing Tab and then Shift+Tab again, or by some other functionality inside the program which, after you've used it, puts the focus back in the relevant pane.

But as you can see, there is no universal method that works in all programs, as far as I know.  My scripts that copy text are all customized for specific programs.

Edited by leuce
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...