-
November 14th, 2009, 03:11 AM
#1
FindWindow
Hi All
I am trying to find excel file is open or not through FindWindow function.
Code:
CWnd *pExal;
pExal = CWnd::FindWindow(NULL, _T("C:\\test.xls"));
if(pExal == NULL){
AfxMessageBox("Couldn't find Excel file");
}
else
{
AfxMessageBox("Excel file is open");
}
In this code if file is open then show nad if not open then also where i am wrong.Plz help me
-
November 14th, 2009, 03:33 AM
#2
Re: FindWindow
does excel state the file that is opened in the window's title? if so, you sure it doesn't contain anything else in the title? like - Microsoft - Excel. Also does it state the directory of the file opened?
In short this isn't reliable enough
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 14th, 2009, 03:36 AM
#3
Re: FindWindow
A quick glance.
I really don't think you have to include C:\ in the window title to search for, so change it to :
Code:
CWnd *pExal;
pExal = CWnd::FindWindow(NULL, _T("Microsoft Excel - test.xls"));
if(pExal == NULL){
AfxMessageBox("Couldn't find Excel file");
}
else
{
AfxMessageBox("Excel file is open");
}
BTW, I think you need to supply Microsoft Excel in the string as well.
Try what I've shown, and report back your results
EDIT, didn't see your response Joe..
-
November 14th, 2009, 06:35 AM
#4
Re: FindWindow
Originally Posted by HanneSThEGreaT
A quick glance.
I really don't think you have to include C:\ in the window title to search for, so change it to :
Code:
CWnd *pExal;
pExal = CWnd::FindWindow(NULL, _T("Microsoft Excel - test.xls"));
if(pExal == NULL){
AfxMessageBox("Couldn't find Excel file");
}
else
{
AfxMessageBox("Excel file is open");
}
BTW, I think you need to supply Microsoft Excel in the string as well.
Try what I've shown, and report back your results
EDIT, didn't see your response Joe..
Sorry for delay response.But nothing change in out put. i use code which is given by you.
Thanks
-
November 14th, 2009, 09:08 PM
#5
Re: FindWindow
What version of Excel are you working with?
-
November 15th, 2009, 11:12 PM
#6
Re: FindWindow
Originally Posted by Arjay
What version of Excel are you working with?
2007 version.
-
November 16th, 2009, 12:07 AM
#7
Re: FindWindow
A good rule to follow when using FindWindow is to include the class name whenever possible. This allows the api to only look for windows matching the class name (and title if specified). When you supply the class name, you increase the chance of finding the correct window. Sure, it's unlikely that any other window will have 'Microsoft Excell' in the title, but it's a good practice to get into. For Excell 2007, the top level window class name is "XLMAIN".
Speaking of Excell 2007, I've noticed that if you open an older spreadsheet (.xls), you'll get "[Compatibility Mode]" as part of the title. If you open the newer spreadsheet format (.xlsx), then this text doesn't appear.
Another interesting thing is that the title text on Excell 2007 seems to appear somewhat reversed with find window compared to how you visually see it.
Visually it appears as "MySheet.xls [Compatibility Mode] - Microsoft Excell", however in code, it appears as "Microsoft Excell - MySheet.xls [Compatibility Mode]". So you'll want to search for the latter.
Code:
HWND hWnd = ::FindWindow( _T("XLMAIN"), _T("Microsoft Excell - MySheet.xls [Compatibility Mode]") );
Keep in mind that you'll want to leave off " [Compatibility Mode]" if searching for the newer style spreadsheet.
-
November 16th, 2009, 04:23 AM
#8
Re: FindWindow
Originally Posted by Arjay
Code:
HWND hWnd = ::FindWindow( _T("XLMAIN"), _T("Microsoft Excell - MySheet.xls [Compatibility Mode]") );
Thanks for reply
How can i use that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|