CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: FindWindow

  1. #1
    Join Date
    Aug 2008
    Posts
    373

    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
    Code:
    pExal == NULL
    nad if not open then also
    Code:
    pExal == NULL
    where i am wrong.Plz help me

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    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

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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..

  4. #4
    Join Date
    Aug 2008
    Posts
    373

    Re: FindWindow

    Quote Originally Posted by HanneSThEGreaT View Post
    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

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FindWindow

    What version of Excel are you working with?

  6. #6
    Join Date
    Aug 2008
    Posts
    373

    Re: FindWindow

    Quote Originally Posted by Arjay View Post
    What version of Excel are you working with?
    2007 version.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  8. #8
    Join Date
    Aug 2008
    Posts
    373

    Re: FindWindow

    Quote Originally Posted by Arjay View Post

    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
  •  





Click Here to Expand Forum to Full Width

Featured