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.
controlsend "ALT" to Excel 2010 not work
Re: controlsend "ALT" to Excel 2010 not work
You could always use a COM method:
EDIT: As COM solutions go, this actually replicates the effect of the shortcut sequence:
Code: Select all
xl := ComObj("Excel.Application")
xl.SendKeys("%",True)
Sleep, 100
xl.SendKeys("oca",True)
Code: Select all
xl := ComObj("Excel.Application")
xl.ActiveCell.Columns.AutoFit
Re: controlsend "ALT" to Excel 2010 not work
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"?
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"?
Re: controlsend "ALT" to Excel 2010 not work
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.