controlsend "ALT" to Excel 2010 not work

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fallmq
Posts: 2
Joined: 22 Oct 2013, 08:19

controlsend "ALT" to Excel 2010 not work

Post by fallmq » 22 Oct 2013, 08:36

I am using AutohotkeyL 1.1.9.3, I want to write a script to auto resize an excel file when opening.
I already know these hotkey string can work on Excel 2010: ctrl + C, then Alt + O, C, A.

below is the header of my script file.
##########################################################
SetBatchLines -1
SetTitleMatchMode, RegEx
SetKeyDelay, 50, 20
file_path = %1%
run, %file_path%
WinWaitActive, Microsoft Excel - *, , 10
sleep, 2000
##########################################################

and below is my code to trigger the hotkey string, I found sendinput() always take effect, but controlsend() sometimes not work.
##########################################################
sendinput, ^a ;work
ControlSend, , !a, Microsoft Excel - ;not work
ControlSend, , {ctrl down}a{ctrl up}, Microsoft Excel - ;work
;sendinput, !oca ;work
ControlSend, , !oca, Microsoft Excel - ;not work
ControlSend, , {alt down}oca{alt up}, Microsoft Excel - ;not work
##########################################################

the result is so strange, I can't explain it, any body can tell me the reason or give me a workaround? thanks a lot.

User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: controlsend "ALT" to Excel 2010 not work

Post by sinkfaze » 22 Oct 2013, 11:20

You could always use a COM method:

Code: Select all

xl :=	ComObj("Excel.Application")
xl.SendKeys("%",True)
Sleep, 100
xl.SendKeys("oca",True)
EDIT: As COM solutions go, this actually replicates the effect of the shortcut sequence:

Code: Select all

xl :=	ComObj("Excel.Application")
xl.ActiveCell.Columns.AutoFit

fallmq
Posts: 2
Joined: 22 Oct 2013, 08:19

Re: controlsend "ALT" to Excel 2010 not work

Post by fallmq » 23 Oct 2013, 02:07

Hi,

firstly thanks for your reply,
your solution work well on my environment.

but I must run "xl.Columns.AutoFit" to trigger the autofit, the "xl.ActiveCell.Columns.AutoFit" not work for all columns, what is the meaning of "ActiveCell"?

User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: controlsend "ALT" to Excel 2010 not work

Post by sinkfaze » 23 Oct 2013, 06:28

The shortcut sequence autofits a particular column based on the cell in that column that has the border around it (or the active cell), hence xl.ActiveCell.Columns.AutoFit. xl.Columns.AutoFit autofits the columns based on the largest cell in each column. As long as one of those solutions works for you. ;)

Post Reply

Return to “Ask for Help (v1)”