0
votes

I've recently wrote a VBScript to send an email using Outlook. Everything works great untill it gets to the sending point. I'm assuming since it only opens a single message, and since the message was sent... the Window closes and Outlook is killed.

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.Display   'To display message
objMail.Recipients.Add ("[email protected]")
objMail.Subject = "Mail Subject"
objMail.Body = "This is Email Body"
objMail.Send
objOutlook.Quit
Set objMail = Nothing
Set objOutlook = Nothing

All the information is entered into the email correctly. However, once the send command is executed, the message is put into the outbox and never sent unless Outlook is opened manually after the fact. Is there a methodology I could use to perhaps run a Send/Recieve before it ends the script? Or a delay that would keep the Outlook process alive for it to actually Send?

I cant seem to think of anything so all help is appreciated.

Thanks

1
Is your outlook set to automaticly send or only every x minutes? - DragonSamu
@DragonSamu, It is currently the default configuration. So I am pretty sure it is set to auto Send/Receive whenever they hit Send - Bryan D
I would suggest using: Application.Wait(Now + TimeValue("0:00:10")) and check for how long outlook should stay open before closing to send the email. then put that time for example 10 seconds like I showed. - DragonSamu

1 Answers

-1
votes

Similar to Application.Wait, except there is no extra delay, nor the possibility of not waiting long enough.

Dim origSentMailCount as Long

origSentMailCount = objOutlook.GetDefaultFolders(olFolderSentMail).Count

objMail.Send

do until objOutlook.GetDefaultFolders(olFolderSentMail).Count > origSentMailCount
doevents
loop