Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Highlight last pasted text


  • Please log in to reply
15 replies to this topic
codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014
In a Word document I need to highlight the text that was last pasted. Nothing else has been copied since so it will be the text that is currently on top of the clipboard (I use Ditto Clipboard). This text will only be in the document once and it will be visible on the screen without scrolling.

GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

If you paste the text at the end of the document use this

Send, {SHIFTDOWN}{F5}{SHIFTUP}
Sleep, 100
Send, {CTRLDOWN}{SHIFTDOWN}{END}{SHIFTUP}{CTRLUP}

Otherwise you could use the Find or Replace command in Word to highlight the text.

 

EDIT:

 

Otherwise this

X := A_CaretX
Y := A_CaretY
Send, {SHIFTDOWN}{F5}{SHIFTUP}
Sleep, 100
Send, {SHIFTDOWN}
Click, %X%, %Y%
Send, {SHIFTUP}

space's solution works faster if you use SendInput:

x:=StrLen(Clipboard)
SendInput {shift down}{Left %x%}{shift up}


space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
After pasting:
Send {shift down}
Loop, % StrLen(Clipboard)
   Send {Left}
Send {shift up}
or
x:=StrLen(Clipboard)
Send {shift down}{Left %x%}{shift up}
?

codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014

Thanks for the help here but it seems I forgot to mention something here as all of these solutions don't work for me for the same reasons. After I paste from the Ditto Clipboard I will be adding more text to the document (None of this added text will be pasted) and some of this added text is what is being highlighted when I run any of these script samples. Is there maybe a way I could start the script right after pasting and that pasted text could be highlighted later I finish adding the needed text. Also if it matters, it turns out the text that I am pasting from Ditto Clipboard does not get placed at the top of the clipboard. 



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
I don't have Office here but I think you can search for the ditto text using the find option
; paste from ditto
Find:=clipboard ; store clipboard text in find
; do other stuff
; now we want to select the pasted ditto text
clipboard:=find
Send ^f
Sleep 100 ; give find window a bit of time to come up
Send ^v ; paste "find"
Send {enter}
Now the text you pasted earlier should be highlighted as I think office will allow you to find it.

codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014

This is closer but there is still a problem and it looks like it might be with word and I can’t figure out a fix. The text that was pasted and is being highlighted with this script is a list with several different lines. The only line that is being highlighted is the first line and I need the entire list highlighted. 



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014

Again I don't have office here, perhaps this would work, you'll have to write the script yourself using these steps:

 

1 - set first bookmark

2 - paste ditto text

3 - set second bookmark

4 - do other stuff

5 - goto first bookmark (either by sending keys OR COM and office),

6 - send shift down

7 - goto second bookmark, send shift up

 

Should work.

 

You may need to look into office & com, search for the hundreds of examples on the forum for some guidance.



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

Maybe:

#IfWinActive ahk_class OpusApp
#s::    ; Win+S to select text in Word that matches the current clipboard contents
FindArr := StrSplit(Clipboard, "`n", "`r")
FirstLine := SubStr(FindArr.1, 1, 250), LastLine := ""
Loop, % FindArr.MaxIndex() - 1  ; Find the last non-blank line
    if (LastLine := SubStr(FindArr.Pop(), -250)) != ""
        break
if (LastLine = "" && StrLen(FindArr.1) > 250)
    LastLine := SubStr(FindArr.1, -250)
wdApp := ComObjActive("Word.Application")
Find := wdApp.Selection.Find
Find.ClearFormatting
Find.Forward := true
Find.Wrap := 1  ; wdFindContinue = 1
Find.Execute(FirstLine)
if (LastLine) {
    RngStart := wdApp.Selection.Range.Start
    Find.ClearFormatting
    Find.Forward := true
    Find.Wrap := 1
    Find.Execute(LastLine)
    RngEnd := wdApp.Selection.End
    wdApp.ActiveDocument.Range(RngStart, RngEnd).Select
}
return

Edited by kon, 04 November 2015 - 03:55 AM.


codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014

Sorry for the slow response. Thanks for the help here. I’m not sure I completely understand space’s solution. Does it assume the pasted text will be 2 lines? The pasted text will actually sometimes be just one line and sometimes maybe 7 or 8 lines. With kon’s solution each line of the pasted text gets highlighted for a brief second starting from the first line and continuing through all of the pasted lines, but I need for all of the pasted lines (1-10 lines) to remain highlighted.  



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
this may not always work
 
f2:: ; paste
oWord := ComObjActive("Word.Application")
Range := oWord.selection.range
Range.paste
return

f3:: ; select
Range.select
return
did not test it much...
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014

Thanks Jackie…. This has been working well or me but it does involve one extra trip to the keyboard and back to the mouse. I’m using Ditto Clipboard and generally will double click the text to be inserted in my word doc. With your solution I have to single click the text and then use the F2 key to paste the text. Can you think of a work around for this?  Thanks



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
Some kind of routine that waits for you to double click within the Ditto Clipboard

; #IfWinActive, Ditto Clipboard windows title
~LButton::
	If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 500)
    {
        sleep 500
		MsgBox You double-Clicked LButton! ; remove when no longer needed
        
        oWord := ComObjActive("Word.Application")
        Range := oWord.selection.range
        Range.paste
    }
Return
; #IfWinActive

f3:: ; select
Range.select
return


esc::ExitApp
Untested
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014

Thanks for the effort here. Without any of this script, when I double click the desired text from Ditto Clipboard it puts it into my word doc. With this latest script, I am getting the desired text then get it again and when I hit F3 it highlights the second set of pasted text. I tried changing some things here but nothing is working. 



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012

You will need to use the #ifWinActive or something like it to get around that issue

 

So something like this

 

#IfWinActive, Ditto Clipboard windows title
LButton::
    If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 500)
{
sleep 500
        MsgBox You double-Clicked LButton! ; remove when no longer needed

oWord := ComObjActive("Word.Application")
Range := oWord.selection.range
Range.paste
}
else
   Send, {LButton}
Return
#IfWinActive

f3:: ; select
Range.select
return

 
esc::ExitApp
you will need to fill in that right windows title of Ditto Clipboard program.

 

to do that you can use the windows spy that comes with the autohotkey install...


Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

codude
  • Members
  • 59 posts
  • Last active: Apr 12 2019 03:07 PM
  • Joined: 30 Dec 2014

Jackie, I’m hoping we can try one more time and if I’m still having problem you had offered a pretty good solution on you very first response that I will go back to until I get a better understanding of AHK. If I am using Window spy correctly it seems the title for the Ditto clipboard is Ditto. On the next #IfWinActive, was I supposed to add a the title of the word doc there?   It seems so because nothing worked without it.  Here is what I ended up with that still is not working. After I hit OK in the message box I hit F3. 

 

#IfWinActive, Ditto

LButton::

    If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 500)

{

sleep 500

        MsgBox You double-Clicked LButton! ; remove when no longer needed

 

oWord := ComObjActive("Word.Application")

Range := oWord.selection.range

Range.paste

}

else

   Send, {LButton}

Return

 

#IfWinActive, Scratch-Pad.docm - Microsoft Word

f3:: ; select

Range.select

return

 

esc::ExitApp