Results 1 to 12 of 12

Thread: Sendkeys not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Sendkeys not working

    I am currently working on Custom Outlook Forms. I am trying to generate the "forward email" action using sendkeys within the script editor window.

    The short-cut for forward email is "CTRL+F". Here's the code below.

    Sub commandButton4_Click
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.SendKeys "^F"
    End sub


    However, when I run the above code, I get an error message "object required :Wscript".

    Please advise is anything is wrong in the above code.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sendkeys not working

    "object required :Wscript".
    you are trying to use wscript to create a wscript object, before wscript is an existing object
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: Sendkeys not working

    Good catch Pete !

    I did not notice this mistake in my code. I made the fix, and now, the error message has stopped appearing. Thanks for this !!

    However, the code is still not forwarding the mail. For some reason, the code executes - yet nothing happens.

    Could you please help me with this...

    Regards,
    Jacob

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Sendkeys not working

    Why SendKeys?
    Use the MAPI-Objects
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: Sendkeys not working

    I am not sure how to use MAPI objects to forward the mail. Just to add - I also tried using Item.forward just like we use item.sent, but for some reason it is not working.

    I would be a great help to me if you could show me how.

    Essentially, I am trying to create a button within my custom outlook form, which will enable the recipient of the mail to forward the mail to another team (post approval).

    Regards,
    Jacob

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sendkeys not working

    Could you please help me with this...
    not enough information!!
    what code are you using?
    what application are you trying to send keys to?

    try sending a non control character, to see if it ends up in someother application, eg. the vba ide
    you need to make sure that the email client is the topmost window, try appactivate immediately before sendkeys

    you can not step through the sendkeys code or the keys will definitely be sent to the vba ide, as it will then be topmost

    if you are using outook or some MAPI mail client, better to automate than rely on sendkeys
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: Sendkeys not working

    Hi Pete,

    Thanks for getting back. Here are the detailed steps of my outlook custom approval form.

    I have modified the message outlook standard template to create a custom approval form. This form essentially requires two levels of approval.

    Step 1 - The preparer fills up the form, and send it to the first approver by clicking the submit button. I was able to do this using the Item. Send method.
    Step 2 - Once the first approver receives the form, the submit button disappears, and the accept and reject buttons appear. I was able to do this using the custom property change event handler.
    Step 3 - If the first approver clicks on the accept button, the form should go to the second approver. If he rejects the form, it must come back to the preparer. This is where I have hit a road block.

    In both these cases (either accept/reject) - If you use the reply method, the message will go, but the form won't. Likewise, you cannot use the send action for a "received mail". So, this leaves us with only one option - a forward action. I tried the "Item.forward" method, but for some reason this does not seem to work at all. This is why, I am trying to see if send keys might just do the trick. I know that sendkeys is not the ideal method of doing it, but I don't have many options left to get the form to bounce off from the recipient. I would be grateful if someone can help me with the VB script code to forward the mail. I am hoping to link this code to the custom accept/reject button. Please advise.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sendkeys not working

    seems like a lot of shag to send an email


    how about like
    Code:
    Set fwd = msg.Forward
    fwd.To = msg.SenderName   ' or  whoever you want to send to
    fwd.Send
    where msg should be the current message, either from ActiveInspector.CurrentItem, ActiveExplorer.Selection (1) or any other code that is returning the message object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: Sendkeys not working

    Hi Pete,

    I am trying to apply the above code for the current message.

    Though, I kind of understand that we use the active inspector method for current items, yet I have not tried this method via code. I know that my code below is incorrect. Would you help me revise it?

    Set objItem = objApp.ActiveInspector.CurrentItem
    Set fwd = objItem.msg.Forward
    Set Sendername ="abc@bcd.com"
    fwd.To = msg.sendername
    fwd.Send

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sendkeys not working

    objitem is the msg
    Code:
    Set fwd = objItem.Forward
    sendername is a string (or variant), not an object, no set keyword required
    why use 2 lines anyway

    or
    Code:
    Set fwd = objApp.ActiveInspector.CurrentItem.forward
    fwd.To = "abc@bcd.com"
    fwd.Send
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: Sendkeys not working

    Hi Pete,

    Thank you for your quick reply. Appreciate your support !

    The below code is working very well when the preparer of the form clicks it. It certainly goes to the first approver. However, once the first approver clicks this button to forward the custom form to second approver, it does not work. Any thoughts?

    Code


    Set objApp = Application (inserted this line because outlook was throwing an error message ("Obj required objapp)

    Set fwd = objApp.ActiveInspector.CurrentItem.forward
    fwd.To = "abc@bcd.com
    fwd.Send

    Awaiting your support.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sendkeys not working

    inserted this line because outlook was throwing an error message
    as you are working in the outlook application that object is not really required at all
    Code:
    Set fwd = ActiveInspector.CurrentItem.forward
    Any thoughts?
    not enough information
    any code of this type would depend on many factors, especially the email client of the recipient and their settings
    as far as i would be aware it could only work if they recipient is using outlook as the mail client

    it would need serious debugging to find out why it works for the first recipient then not for the second if both are using outlook
    you would need to create a log file of what, if any, parts of the code are working, then get that log file sent back to you

    if you want any further help here, you probably need to post all the codes that are used in your custom form and any other information that might be relevant
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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