CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2006
    Posts
    148

    SendMessage to Excel

    Hi, all

    Is it possible to send 'CTRL+A' and 'CTRL+C' to MS Excel using SendMessage?

    I have tried WM_KEYDOWN and WM_KEYUP Messages for 'CTRL+A' with following code but no success.

    Code:
    SendMessage(m_fEC2,WM_KEYDOWN, (WPARAM)VK_CONTROL,(LPARAM)1);
    SendMessage(m_fEC2,WM_KEYDOWN, (WPARAM)65,(LPARAM)1);
    SendMessage(m_fEC2,WM_KEYUP, (WPARAM)VK_CONTROL,(LPARAM)1);
    SendMessage(m_fEC2,WM_KEYUP, (WPARAM)65,(LPARAM)1);
    m_fEC2 is the handle to the Child window as detected in SPY++.

    Difference from SPY++ is only the LPARAM Parameter. How to construct LPARAM parameter for the same? or is there any thing wrong or missing.

    Thanx for any kind of help.

    Regards,
    Shail2k4

    Regards,

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: SendMessage to Excel

    Try SendInput instead.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2006
    Posts
    148

    Re: SendMessage to Excel

    Quote Originally Posted by VictorN View Post
    Try SendInput instead.
    Thanx for the reply..

    Will it be useful when the windows is not having focus?

    In my case the window may not be having focus.

    Regards,
    Shail2k4

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: SendMessage to Excel

    Nope... SendInput inserts your commands into the keyboard chain, so it is the same as you pressing the keys yourself. I guess the only thing you can do is set the focus to excel before you send the keys, but I don't think that you want that

  5. #5
    Join Date
    Jun 2006
    Posts
    148

    Re: SendMessage to Excel

    Quote Originally Posted by Skizmo View Post
    Nope... SendInput inserts your commands into the keyboard chain, so it is the same as you pressing the keys yourself. I guess the only thing you can do is set the focus to excel before you send the keys, but I don't think that you want that
    Absolutely i cant do that..

    What is wrong with the SendMessage?

    Again i made a test with the notepad still no success...

    Thanx for any kind of help..
    Regards,
    Shail2k4

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

    Re: SendMessage to Excel

    SendMessage isn't a good choice because it's synchronous so if the target app is hung, your app will hang. SendMessageTimeout would be a better choice.

    Unfortunately, it still won't work in this case where you are doing a select all/copy to the clipboard.

    If you want to do this, you'll need to set focus on the target application (SetForegroundWindow) and then use SendInput as Victor suggested.

    An alternative to this is to use Excell's automation model to connect to the active Excell instance and access the work book from there.

    If you can give more specifics on what you are actually trying to do, then we might be able to offer additional alternatives.

  7. #7
    Join Date
    Jun 2006
    Posts
    148

    Re: SendMessage to Excel

    Quote Originally Posted by Arjay View Post
    SendMessage isn't a good choice because it's synchronous so if the target app is hung, your app will hang. SendMessageTimeout would be a better choice.

    Unfortunately, it still won't work in this case where you are doing a select all/copy to the clipboard.

    If you want to do this, you'll need to set focus on the target application (SetForegroundWindow) and then use SendInput as Victor suggested.

    An alternative to this is to use Excell's automation model to connect to the active Excell instance and access the work book from there.

    If you can give more specifics on what you are actually trying to do, then we might be able to offer additional alternatives.
    Thanx for the reply
    Here is the specification of the work that i want to do.

    I am having an external application from which i need to capture the data.

    There are two possible source of the data.
    One is the window with TStringGrid and another is a window containing embedded excel object.

    Thanx for any kind of help.

    Regards,
    Shail2k4

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

    Re: SendMessage to Excel

    SetForegroundWindow and SendInput will get the job done as long as setting focus on the application isn't a problem.

  9. #9
    Join Date
    Dec 2008
    Posts
    114

    Re: SendMessage to Excel

    SendMessage works perfectly by attaching the remote thread

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

    Re: SendMessage to Excel

    Quote Originally Posted by carl666 View Post
    SendMessage works perfectly by attaching the remote thread
    Not recommended. If you use SendMessage to send a message to an application that is hung, you will hang your app. Try it - just create a simple dialog app, add a button and in the button handler, add a Sleep( 100000 ) command to simulate a hung app. Open the test app, press the button and then use SendMessage from another app to send it a message. The 'sending' app will hang.

  11. #11
    Join Date
    Jun 2006
    Posts
    148

    Re: SendMessage to Excel

    Quote Originally Posted by Arjay View Post
    Not recommended. If you use SendMessage to send a message to an application that is hung, you will hang your app. Try it - just create a simple dialog app, add a button and in the button handler, add a Sleep( 100000 ) command to simulate a hung app. Open the test app, press the button and then use SendMessage from another app to send it a message. The 'sending' app will hang.
    In this condition can i make use of PostMessage instead of SendMessage?

  12. #12
    Join Date
    Jun 2006
    Posts
    148

    Re: SendMessage to Excel

    Quote Originally Posted by carl666 View Post
    SendMessage works perfectly by attaching the remote thread
    I have tried doing the same but no success.

    Can u pls provide me some working example?

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

    Re: SendMessage to Excel

    Quote Originally Posted by shail2k4 View Post
    I have tried doing the same but no success.
    Have you tried using SetForegroundWindow and SendInput?

  14. #14
    Join Date
    Jun 2006
    Posts
    148

    Re: SendMessage to Excel

    Quote Originally Posted by Arjay View Post
    Have you tried using SetForegroundWindow and SendInput?
    I have tried and its working fine but if the target window is minimized then it is not working.

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

    Re: SendMessage to Excel

    Quote Originally Posted by shail2k4 View Post
    I have tried and its working fine but if the target window is minimized then it is not working.
    Would you expect to be able to type into an app that has focus but is minimized?

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