0

I really don't know much about VB script, and I could use some help with this little problem.

I'm trying send an email once a certain script is run every morning (a scheduled task). Now, our email server is configured to prevent sending automated emails - a feature I generally appreciate - and so I need to simulate a keystroke to acknowledge a warning box and actually send the email.

Here's the script I have so far:

Sub SendEmail_Outlook()
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set ol=CreateObject("Outlook.Application") 
    Set Mail=ol.CreateItem(0) 
    Mail.to= "X@xyz.com"

    Mail.Subject =  "Subject"
    Mail.HTMLBody = "Body"
    Mail.Display    

    WScript.Sleep 1000
    WshShell.SendKeys "%s"
    Set Mail = Nothing 
    Set ol = Nothing 
End Sub

SendEmail_Outlook

The script works like a charm, but only if I'm logged in. If I'm not logged in, the email draft is prepared, the window is activated, but the email is not actually sent. I'm assuming it's because the simulated keystroke does not work if no one's logged in?

So, the question is: is there a way to "tweak" this script to make it run even when no one's logged in?

Many thanks, help would be appreciated!

Philipp

PMaier
  • 494
  • 4
  • 16
  • What do you mean by _not logged in_? Do you mean if outlook is not opened? I tried your script without having the outlook application open and it worked just fine. – Pankaj Jaju Mar 07 '14 at 14:23
  • 1
    You should have a look at [this](http://stackoverflow.com/questions/7041938/vbscript-to-send-email-without-running-outlook) too – Pankaj Jaju Mar 07 '14 at 14:30
  • Let me clarify: Outlook is oopen, but I'm not logged into the PC at all. The script is supposed to run in the middle of the night.... – PMaier Mar 07 '14 at 15:59
  • Did you check out the link i mentioned in my earlier comment? – Pankaj Jaju Mar 07 '14 at 16:01
  • Pankaj: Let me also add that I do **not** know the SMTP address of the server. – PMaier Mar 07 '14 at 16:18

2 Answers2

0

Did you check the vbscript [info] section? It explains how cscript.exe can execute vbscript while the user is not logged on.

"Note: Scheduled VBScript tasks succeed under Cscript.exe due to running the script as a console application rather than a windows application. Computer/Domain policies limit activation of windows applications while no user is logged on."

Edit:

Activate the sending of email over the .Send property rather than using the sendkeys method.

Sub SendEmail_Outlook()
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set ol=CreateObject("Outlook.Application") 
    Set Mail=ol.CreateItem(0) 
    Mail.to= "X@xyz.com"

    Mail.Subject =  "Subject"
    Mail.HTMLBody = "Body"
    Mail.Display    

    WScript.Sleep 1000
    '-----------
    Mail.Send
    '-----------
    Set Mail = Nothing 
    Set ol = Nothing 
End Sub

SendEmail_Outlook

Further information on automating emails directly too the outlook application can be referenced here: Link

Community
  • 1
  • 1
Rich
  • 3,751
  • 2
  • 23
  • 43
  • Rich - I've read it, slept a night, re-read it, and I still don't think I fully understand what it means. It's the second sentence that I find puzzling. I *think* the implication is that it cannot be done! or am I wrong? Thanks - Philipp – PMaier Mar 10 '14 at 02:22
  • No i believe it can be done. Save your script above as a .vbs file and make a batch file containing "c:\windows\system32\cscript.exe c:\your\script\location.vbs" and set it as a schedule task at night. Leave your outlook open and lock the pc. Everything should go as planned. – Rich Mar 10 '14 at 07:17
  • Rich - that is exactly what I've been doing, but it doesn't work. The message is prepared, but not actually sent. – PMaier Mar 10 '14 at 13:40
  • That makes sense. It's probably activating the .SendKeys command to the lock screen rather than the outlook application. To fix this, you should activate the .Send property directly off of the mail object rather than the sendkeys, please view my revised answer above. – Rich Mar 10 '14 at 14:53
  • Rich, thanks for your help. But, now I'm back to where I started: I get a dialog box "A program is trying to send an email on your behalf..." - and I need to click on "Allow" (which I don't think can be automated)... :-( – PMaier Mar 10 '14 at 15:37
  • Hey no problem man. But this is the easy part! There is a program called "ClickYes" which will suppress these messages. View here: http://www.contextmagic.com/express-clickyes/ – Rich Mar 10 '14 at 15:49
0

With the help of a colleague, I think I found the answer - at least it's an option I've implemented and it seems to run successfully so far. As this may be of interest to others as well, there's what we have done:

  1. Download and install "Outlook Redemption" here: http://www.dimastr.com/redemption/home.htm. No admin rights required.
  2. Use the following script:

    Sub SendEmail_Outlook() Set WshShell = WScript.CreateObject("WScript.Shell") Set oApp=CreateObject("Outlook.Application") Set NS = oApp.GetNamespace("MAPI") NS.Logon Set SafeItem = CreateObject("Redemption.SafeMailItem") Set oMailItem = oApp.CreateItem(0)

    SafeItem.Item = oMailItem
    SafeItem.To = "x@yz.com"
    SafeItem.Subject = "Subject"
    SafeItem.BodyFormat = 2
    SafeItem.HTMLBody = "Body"
    SafeItem.Send   
    SafeItem = Nothing    
    Set oMailItem = Nothing    
    Set NS = Nothing
    Set oApp = Nothing
    

    End Sub

    SendEmail_Outlook

As indicated, the script has been running smoothly, despite getting an error at the end, saying that:

VBScript runtime error: Object variable not set: 'SafeItem'

But, the email arrives, so the error doesn't really bother me... ;-)

PMaier
  • 494
  • 4
  • 16