Paste Clipboard contents into A1 Cell in Excel

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NackJich
Posts: 22
Joined: 30 Jul 2017, 06:34

Paste Clipboard contents into A1 Cell in Excel

20 Jan 2019, 17:27

This has me beat (no surprise there!)
I'm trying to get the mouse to go to A1, click it, and then paste the clipboard to the cell.
As it stands, when I press x, the mouse doesn't even move.
Wonder if someone could help, please?

Code: Select all

WinActivate, Book1 - Excel ahk_class XLMAIN
 #SingleInstance Force
 x::

{
CoordMode, MouseMove,61,283, Screen
MouseClick, left

 
InputBox,OutputVar,Paste clipboard content
sendclipboard:
  controlSend,,^v,Paste clipboard content
return
 
}
 
return
Ridwan
Posts: 144
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: Paste Clipboard contents into A1 Cell in Excel

20 Jan 2019, 18:26

Maybe it's because of this 2 commands be in the same line,

Code: Select all

CoordMode, MouseMove,61,283, Screen

Try this one:

Code: Select all

WinActivate, Book1 - Excel ahk_class XLMAIN
x::
    CoordMode, Mouse, Screen
    MouseMove,61,283
    MouseClick, left

    InputBox,OutputVar,Paste clipboard content
    sendclipboard:
      controlSend,,^v,Paste clipboard content
    Return 
Return
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: Paste Clipboard contents into A1 Cell in Excel

21 Jan 2019, 01:35

I'm afraid there is no Targettype parameter (which is the first parameter of Coordmode) called Screen according to the docs.
But you could do

Code: Select all

Coordmode, Mouse, Screen	; or
Coordmode, Mouse	; Same as above because "screen" is the default for Coordmode second parameter.
This part I don't understand:

Code: Select all

InputBox,OutputVar,Paste clipboard content
sendclipboard:
  controlSend,,^v,Paste clipboard content
Why an inputbox here, especially if you do nothing with the input that gets saved in Outputvar?
I would rather use just Send instead of Controlsend since - at this point - you already clicked into the cell. Well, okay, the inputbox might take this focus away, but just remove it, if you have no use for it. Also, the Wintitle "Paste clipboard content" that you use here with Controlsend will probably prevent that ^v gets sent at Excel at all (and the Inputbox with this wintitle is already closed at this point anyway).

Some additional thoughts:
You could also think about pulling the line WinActivate, Book1 - Excel ahk_class XLMAIN into the hotkey definition, depending on your actual work flow, so that the window always gets activated before you try to paste something. But perhaps you would rather want a context-sensitive hotkey instead...
Finally, Excel has also a COM interface which would give you a more reliable method to to the pasting. Just some ideas to look up in the docs and the forum...
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

Re: Paste Clipboard contents into A1 Cell in Excel

21 Jan 2019, 02:59

Any reason you want the mouse click instead of using COM to directly connect to excel and send the contents of your clipboard to cell A1?

Code: Select all

Xl := ComObjActive("Excel.Application") ;creates a handle to your currently active excel workbook
Xl.Range("A1").Value := Clipboard	; Copy  contents of clipboard variable into an Excel cell
A great starter tutorial on using excel COM can be found here:
https://autohotkey.com/board/topic/69033-basic-ahk-l-com-tutorial-for-excel/

Also, if you use COM, you don’t have to have the excel window in the foreground for it to work. You could still use your inputbox and not even have to switch to the Excel window for the text to get added to the cell.
NackJich
Posts: 22
Joined: 30 Jul 2017, 06:34

Re: Paste Clipboard contents into A1 Cell in Excel

22 Jan 2019, 14:49

Hi .. thanks for your answers. I messed about with this endlessly last night, and in the end I got something that works. Very simple... activates excel by clicking on it, not ideal I know.
Click, 1128, 614, 0
Sleep, 140
Click, 1127, 615, 0
Sleep, 47
Click, 1126, 615, 0
Sleep, 47
Click, 1126, 614, 0
Sleep, 63
Click, 1127, 614, 0
Sleep, 31
Click, 1126, 615, 0

This (above) is an example of what I am trying to paste. In certain circumstances the paste goes into just one cell (A1). Other times it goes into a range of cells which is what I wanted. I wonder if com works with my version of AHK: 3.0.0.6? Anyway this is what works and it's simplicity itself (I am very new to this).

Code: Select all

#SingleInstance Force
Sleep 200
CoordMode, Mouse, Screen
    MouseMove,61,275
	Sleep 50 ; wait a bit
    MouseClick, left
Sleep 600 
Send ^v
Sleep 400
Thanks for your help again. The input box was there just because it was copied/pasted from something else I found. I pinched bits from here and there !!
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

Re: Paste Clipboard contents into A1 Cell in Excel

22 Jan 2019, 21:55

If you’re going to go the mouse click route, make sure to put in a
Winmaximize, ahk_class XLMAIN

at the beginning of your script. That way it will maximize your excel window and always be in the same position.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 286 guests