Excel Ribbon sometimes changes when window is activated

Get help with using AutoHotkey and its commands and hotkeys
codude
Posts: 113
Joined: 12 Nov 2015, 05:33

Excel Ribbon sometimes changes when window is activated

25 Nov 2020, 18:02

Hello, In some script I have “WinActivate, Inventory.xlsm” followed by “WinWaitActive, Inventory.xlsm”. Often, when running this (maybe once every 5-10 times) the Ribbon tab somehow gets changes from “Home” to “File” and this completely misses up the rest of the script. I have been adding a message box in different areas of the script to determine where this is going wrong. It seems that if I have this message box right after WinWaitActive, Inventory.xlsm the Excel file always opens correctly on the home tab after clearing the MsgBox. Is it possible that the Excel file is not ready to work after it becomes active and if so is there a way to make sure that it is ready other than a time delay? I tried replacing the message box with Sleep 500 and it did not fix the problem. Any ideals here? I don’t know if it matters but there are a couple different Excel files running in one instance of Excel. Thank you
User avatar
mikeyww
Posts: 3965
Joined: 09 Sep 2014, 18:38

Re: Excel Ribbon sometimes changes when window is activated

25 Nov 2020, 18:09

I had no trouble with the following, but this does not necessarily apply to how your script works-- what commands are being used after the activation, etc.

Code: Select all

F3::
SoundBeep, 1500, 20
WinActivate, Book1.xlsx
SendInput 4{Enter}
Return
If you are using WinWaitActive immediately after the program is run, then it could cause an issue (though it's still recommended), because the window might still not be ready for input immediately. I have seen many programs with windows like that. In some cases, a short sleep is then still needed. Related wisdom.

The following script did work.

Code: Select all

F3::
file = Book1.xlsx
SoundBeep, 1000, 30
Run, %TEMP%\%file%
WinWaitActive, %file% ahk_exe EXCEL.EXE,, 10
If ErrorLevel
 MsgBox, 48, Error, The window did not become active.
SoundBeep, 1500, 30
SendInput {Text}92
SendInput {Enter}
Return
If you post your script, it would help with the debugging.
User avatar
tank
Posts: 2853
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Irving TX
Contact:

Re: Excel Ribbon sometimes changes when window is activated

25 Nov 2020, 18:55

so some thoughts of an idiot.

things to insure your script is consistent.
  • is there a shortcut excel key combo to activate home? :idea:
  • is there cell selected or none. sending keys when not selected. wonder what would happen if not :idea:
  • have you considered using COM :idea:
now i have no clue the specifics of your project. what i do know is that nothing is perfect and you may need some error handling to recover when things go wrong. i also know that there are several libraries for COM based excel interaction in this forum. this removes your need to understand the actual COM aspect. and i know that with COM the window doesnt need to be active or even visible. and the ribbon doesnt matter in that case.

but im an idiot with little experience on this subject
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
Check Out WebWriter
Thanks Tank :thumbup:
User avatar
mikeyww
Posts: 3965
Joined: 09 Sep 2014, 18:38

Re: Excel Ribbon sometimes changes when window is activated

25 Nov 2020, 19:06

Those are all great points and suggestions!

https://autohotkey.com/board/topic/69033-basic-ahk-l-com-tutorial-for-excel/

There are also lots of posts with Excel COM examples.
codude
Posts: 113
Joined: 12 Nov 2015, 05:33

Re: Excel Ribbon sometimes changes when window is activated

25 Nov 2020, 19:27

Thank you Tank... Your ideal to have some error handling to recover when things go wrong seems like a good way to deal with this. A Alt+H key shortcut key would generally bring up the home tab unless the"File" window is selected in which case it brings up a "Help" window. Let me explore some of the COM options you mention.
User avatar
tank
Posts: 2853
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Irving TX
Contact:

Re: Excel Ribbon sometimes changes when window is activated

26 Nov 2020, 02:42

when you have something coded feel free to post it and we can work on making it bullet proof.. my goal is to help you learn but when i see code i generally get a bit less vague.
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
Check Out WebWriter
Thanks Tank :thumbup:

Return to “Ask For Help”

Who is online

Users browsing this forum: 20170201225639, HiSoKa, IceBubble, mikeyww, necomerx, RyantheCoder, viv and 38 guests