Page 1 of 1

Copy, Switch Tabs, paste, switch tabs, repeat

Posted: 17 Dec 2020, 17:58
by stevers
Hey,

I'm a noob to AHK, and I am trying to automate the following sequence, which copies and pastes data from Excel into a form.

Copy (from Excel)
Alt+Tab (switch to browser)
Paste
Tab (move to next field in browser)
Alt+Tab (switch back to Excel)
Tab (move to next record in Excel)
----Repeat above code 2 times, then perform this procedure:

Copy (from Excel)
Alt+Tab (switch to browser)
Paste
Tab (move to next field in browser)
12
Tab
17
Tab
2020 (putting in the date, I can just update the values in the script as needed)
Tab (move to next row in browser)
Alt+Tab (switch back to Excel)
Enter
Enter (Moves to next line in Excel)

-----Repeat all above code 10 times

Here's the code that I tried to get my copy/paste to work, but it didn't seem to function properly:

Code: Select all

^0::
Send, ^c
Send, {Alt down}{tab}
Send, {Alt up}
Send, ^v
Return
Any suggestions on how to get this syntax right? And how to do loops?

Re: Copy, Switch Tabs, paste, switch tabs, repeat

Posted: 17 Dec 2020, 19:23
by mikeyww
It looks like you stopped after the first few lines. When you ran your script and pressed Ctrl+0, what happened?

Re: Copy, Switch Tabs, paste, switch tabs, repeat

Posted: 17 Dec 2020, 20:32
by mshafer1
Here's a straight translation of your steps, I would suggest debugging by setting the loops to 1 and putting in a "Return" to terminate early and slowly move it down as you see that each step is working properly

Code: Select all

Month := 12
Day := 17
Year := 2020


^0::
	Loop, 2
	{
		Send, ^c ; Copy (from Excel)
		Sleep, 500 ; (give the clipboard time to capture it)
		Send, !{Tab}  ; Alt+Tab (switch to browser)
		send, ^v  ; paste
		Sleep, 500  ; (give the clipboard time to input)
		Send, {Tab}  ; Tab (move to next field in browser)
		Send, !{Tab}  ; Alt+Tab (switch back to Excel)
		Send, {Tab}  ; (move to next record in Excel)
	} ; Repeat above code 2 times, then perform this procedure:

	Loop, 10
	{

		Send, ^c  ; Copy (from Excel)
		Sleep, 500

		Send, !{Tab}  ; Alt+Tab (switch to browser)
		
		Send, ^v  ; Paste
		Sleep, 500

		Send, {Tab}  ; (move to next field in browser)
		Send, %Month%{Tab}%Day%{Tab}%Year%{Tab} ; (putting in the date)

		Send, {Tab}  ; (move to next row in browser)
		Send, !{Tab}  ; Alt+Tab (switch back to Excel)
		Send, {Enter}  ; Enter
		Send, {Enter}  ; Enter (Moves to next line in Excel)
	}  ; Repeat all above code 10 times
	Return



Re: Copy, Switch Tabs, paste, switch tabs, repeat

Posted: 18 Dec 2020, 00:10
by stevers
This is super helpful. Thanks! I'm starting to learn how some of the syntax works. However, I seem to be having trouble getting things to actually copy from Excel (or paste into the browser).

I tried modifying the script to add in some Excel commands that I saw others using on the forum, but they didn't help me. Any tips on what I'm doing wrong here?

Code: Select all

^0::
WinActivate, ahk_exe EXCEL.EXE
oExcel := ComObjActive("Excel.Application")
oExcel.Visible := true

	Loop, 2
	{
		oExcel.ActiveCell.Select
		Send, ^c ; Copy (from Excel)
		Sleep, 500 ; (give the clipboard time to capture it)
		Send, !{Tab}  ; Alt+Tab (switch to browser)
		send, ^v  ; paste
		Sleep, 500  ; (give the clipboard time to input)
		Send, {Tab}  ; Tab (move to next field in browser)
		Send, !{Tab}  ; Alt+Tab (switch back to Excel)
		oExcel.ActiveCell.Offset(0, 1).Select
	} ; Repeat above code 2 times, then perform this procedure:

	Loop, 1
	{
		Send, ^c  ; Copy (from Excel)
		Sleep, 500
		Send, !{Tab}  ; Alt+Tab (switch to browser)
		Send, ^v  ; Paste
		Sleep, 500
		Send, {Tab}  ; (move to next field in browser)
		Send, %Month%%Day%%Year%; (putting in the date)
		Send, {Tab}  ; (move to next row in browser)
		Send, !{Tab}  ; Alt+Tab (switch back to Excel)
		oExcel.ActiveCell.Offset(-1, -2).Select
	}  ; Repeat all above code 10 times
	Return
Oh, and any tips on how to nest loops? I actually need the first loop to run twice, the second loop to run once (so not really a loop), and then the whole thing to loop 10 times. Do I need to pay attention to indenting, like with Python? Or just make sure I get my curly braces right?