AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WinActivate / WinWaitActive operating on wrong Excel Window!

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
SAbboushi



Joined: 18 Sep 2004
Posts: 60

PostPosted: Mon Feb 21, 2005 8:48 pm    Post subject: WinActivate / WinWaitActive operating on wrong Excel Window! Reply with quote

OK - I am going nuts here Confused

I have 2 Excel 2003 workbooks open:

1) TEMP FOR METASTOCK SYSTEM TEST PASTE.xls
2) No Criteria - Downward Trend.xls

Code:

SetTitleMatchMode, 2
WinGetTitle, Title, A
MsgBox, The active window is "%Title%".
   WinWait, TEMP FOR METASTOCK SYSTEM TEST PASTE.xls, , 2
   if ErrorLevel <> 0
   {
      MsgBox, WinWait timed out. (TEMP FOR METASTOCK SYSTEM TEST PASTE.xls)
      exit
   }
   IfWinNotActive, TEMP FOR METASTOCK SYSTEM TEST PASTE.xls, , WinActivate, TEMP FOR METASTOCK SYSTEM TEST PASTE.xls,
   WinWaitActive, TEMP FOR METASTOCK SYSTEM TEST PASTE.xls,
   
WinGetTitle, Title, A
MsgBox, The active window is "%Title%".


This code sometimes opens the WRONG WINDOW! I will sometimes find that the window opened is "No Criteria - Downward Trend.xls" even though the last MsgBox of my code displays:
Quote:

The active window is "TEMP FOR METASTOCK SYSTEM TEST PASTE.xls".


Any ideas why?!

Further details:
Excel 2003 does some odd stuff regarding windows:

1) Opening 1 workbook results in one Excel window being open. Title is "Microsoft Excel - " prepended to the workbook name (e.g. "Microsoft Excel - TEMP FOR METASTOCK SYSTEM TEST PASTE.xls"

2) Opening 2 workbooks results in 3 Excel windows being open. With my two workbooks, this results in the following Window Titles:
a) "Microsoft Excel - TEMP FOR METASTOCK SYSTEM TEST PASTE.xls";
b) "TEMP FOR METASTOCK SYSTEM TEST PASTE.xls"; and
c) "No Criteria - Downward Trend.xls".

Whichever was the LAST Excel window to be opened has 2 windows associated with it according to Program Manager (and wingettitle) - one window has the workbook name as the window title ('b' above), the other window has the workbook name prepended with "Microsoft Excel -" as the window title ('a' above).

3) Each of the 3 windows has the SAME Unique Window ID according to
Code:
#T::
WinGet, WindowID, ID, A
Msgbox, Window ID: %WindowID%


Based upon this, I am thinking that Excel does some strange things with Windows titles which may be the cause of my inconsistent results with WinActivate and Excel.

Does anyone know how to help me write reliable code that ensures the correct Excel workbook is opened? Embarassed

Thanks...
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Tue Feb 22, 2005 12:53 am    Post subject: Reply with quote

Excel and PowerPoint both seem to use unusual windowing methods. Try using Control-Tab to toggle different spreadsheets until the right one becomes active:
Code:
WinActivate, ahk_class XLMAIN  ; Activate one of them.
Loop 10  ; Limit it to avoid an infinite loop when spreadsheet doesn't exist.
{
   IfWinActive, Microsoft Excel - The desired spreadsheet's title
      break
   ; Otherwise, keep going through the spreadsheets until the right one is found:
   Send ^{tab}
}
There is probably an easier way, perhaps with SendMessage, but that would require some research.
Back to top
View user's profile Send private message Send e-mail
SAbboushi



Joined: 18 Sep 2004
Posts: 60

PostPosted: Tue Feb 22, 2005 3:17 am    Post subject: Reply with quote

Thanks Chris - your code was more reliable, but the wrong window still popped up occasionally.

3 observations:

1) It seems that WinActivate is not reliable with Excel - it won't always activate the correct Excel window

2) I found that IfWinActive was more reliable if the window is activated with the code you provided:

Code:
WinActivate, ahk_class XLMAIN


I tried to "improve" your code by specifying the specific window I wanted activated - while using your loop to confirm that the right window was in fact activated. My thinking: Since my original WinActivate worked MOST of the time, I figured I would still specify the correct window - and during the times that the correct window was correctly activated, your code would "break" during the first loop. But SURPRISE - specifying a specific window seems to cause IfWinActive to report unreliable information!! Even so, it was still occasionaly unreliable - so I've concluded that I can only have one Excel workbook open at a time when trying to activate with AHK - and to use range names to navigate between worksheets.

3) You are admirably diligent at taking good care of us loyal users out here. Thanks! Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group