+ Reply to Thread
Results 1 to 14 of 14

Application.SendKeys stopped working

  1. #1
    Registered User
    Join Date
    03-11-2005
    Posts
    87

    Application.SendKeys stopped working

    Hi all,

    getting a bit of headache from this...

    I have a simple macro that from Outlook Express sends emails, with the last code line to mimic ALT+S on the keyboard to send the message

    Application.Wait (Now + TimeValue("0:00:02"))
    Application.SendKeys "%s"

    Yesterday I needed to install some new software that required at the end reinstalling of Microsoft Redistributable 2005. Not sure if for that reason but now the last line (the SendKeys) is not sending the email automatically anymore. What happens is:
    -the email message windows pops up but is not being sent unless i do it manually (if i get rid of the two seconds pause ahead of that)
    -have a VB error message that takes me before the SendKeys line (if I keep the two secs wait ahead of that)

    It looks to me that either:
    -the pc is not active anymore on the email message therefore the SendKeys doesnt work
    -somehow the instruction finds the "door" shut by something else
    -worse of all, the software has messed up with those send keys commands (tried to reinstall it but to no avail)

    Any ideas, suggestions please?

    Many thanks

    Bondcrash

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Firstly, please use Code Tags when posting code to the Forum.

    Secondly, avoid Sendkeys, it often causes problems.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Bondcrash,

    This is a more reliable method.

    Place this code in a Standard VBA Module
    Please Login or Register  to view this content.
    Add this Code to your Macro
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  4. #4
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    Thanks a lot Leith. Getting close, I think. The code is as follows. This way is not running. Where would you put the DoEvents?

    Best
    BC


    Sub Mail_Outlook_Express_Out()

    Dim Recipient As String, Subj As String, HLink As String
    Dim Recipientcc As String, Recipientbcc As String
    Dim Msg As String
    Recipient = "xxx@yyy.com"
    Recipientcc = "yyy@xxx.com"
    Recipientbcc = ""
    Subj = Subj & Cells(15, 66).Text
    Msg = WorksheetFunction.Substitute(Msg, vbNewLine, "%0D%0A")
    HLink = "mailto:" & Recipient & "?" & "cc=" & Recipientcc _
    & "&" & "bcc=" & Recipientbcc & "&"
    HLink = HLink & "subject=" & Subj & "&"
    HLink = HLink & "body=" & Msg
    ActiveWorkbook.FollowHyperlink (HLink)

    DoEvents

    'Wait for Outlook Express window to show
    If Subj = "" Then Subj = "New Message"
    While Ret = 0
    Ret = FindWindow(vbNullString, Subj)
    Wend

    'Wait 2 seconds before sending keystrokes (for settling)
    Application.Wait (Now + TimeValue("0:00:02"))

    'Send the email
    Application.SendKeys "%s"


    End Sub

  5. #5
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    I just checked.. the code Leth suggested works spot on calling the routine directly but not if it is being called by another routine. any suggestion???

    Many thanks
    BC

  6. #6
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    Ok - it works without any two seconds pause - I am just wondering how stable is going to be - it is very very quick - will it be missing emails?

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Bondcrash,

    The DoEvents should be executed before the loop looking for the Outlook Express window. Not sure why it isn't working with the delay. Should be stable, but check periodically to see if any emails fail to be sent.

    Sincerely,
    Leith Ross

  8. #8
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    Hi Leith,

    sorry I am pestering on this. I just experienced the following:

    -only way to get it running when it is called by another routine is getting rid of the two seconds wait

    -however after trying to call it on his own twice in rapid succession the system became quite unstable showing a message of "non enough resources to display" the sheet which is non-sense on this pc.

    What do you think?

    Best
    BC

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I would seriously recommend that you wrap your code with code tags before a moderator locks your post!

  10. #10
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    Is the FindWindow function useful to myself only?

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Bondcrash,

    Sorry, what do you mean by "useful only to yourself"?

    Thanks,
    Leith

  12. #12
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    what i mean somebody else must have come across the same issue with SendKeys "misbehaving"

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Bondcrash,

    I did a few years back when I was writing an application program. I am sure this happens to a lot of people because there are many problems associated with SendKeys. Now, I prefer to Outlook to send my emails, fewer problems and more robust functionality.

    Sincerely,
    Leith Ross

  14. #14
    Registered User
    Join Date
    03-11-2005
    Posts
    87
    Anyway thanks for your help LR. Agree on Outlook but I am on tight schedule so i am forced to stick to Express (sigh). I'll keep an eye on the stability issue without the two secs pause

    Best

    BC

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1