Edrei
Edrei

Reputation: 11

Quit Outlook object AFTER email is sent

Here is the code I have so far:

Option Explicit

Call OpenOutlook()

Function OpenOutlook()
    Dim ObjShell

    Set ObjShell = CreateObject("WScript.Shell")
    ObjShell.Run("Outlook.exe")

    Call SendEmail()

    'I tried closing from here but this didn't work either
    'ObjShell.Quit
End Function

Function SendEmail()
    'Declaring variables used through out this function
    Dim ObjOutlook
    Dim objMail

    Set ObjOutlook = CreateObject("Outlook.Application")
    'CreateItem(0) opens a New Email window...MailItem
    set objMail = ObjOutlook.CreateItem(0)
    objMail.Display 

    'MailItem Options
    objMail.to = "[email protected]"    
    'objMail.cc = "[email protected]"
    objMail.Subject = "Did it work!?"
    objMail.Body = "If you got this email, my VBs test worked!"
    'objMail.Attachments.Add("C:\Attachment\abc.jpg")
    objMail.Send

    'This didn't work either
    'If objMail.Sent = True Then 
    'ObjOutlook.Quit
    'End If

    'Quit closes Outlook like I want but it doesn't wait for the email to send 
    'ObjOutlook.Quit
End Function

What I'm trying to automate using VBScript:

  1. Open Outlook
  2. Send an email
  3. Wait for email to send (Outbox to finish sending)
  4. Close Outlook AFTER the email has been sent

Where I'm stuck:

  1. First of all, I was having trouble opening Outlook. Below is the code that I used to create an Outlook Object:

    Set ObjOutlook = CreateObject("Outlook.Application")
    'CreateItem(0) opens a New Email window...MailItem
    set objMail = ObjOutlook.CreateItem(0)
    objMail.Display 
    

    What I did (Not even sure if this is the right way to do it):

    Set ObjShell = CreateObject("WScript.Shell")
    ObjShell.Run("Outlook.exe")
    

    Why can't I just do ObjShell.Quit after I call the SendEmail() Function? Using .Quit gives me an error.

  2. I just want to close the Outlook application once the email has been sent and I can't figure out how.

Upvotes: 1

Views: 2915

Answers (3)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

Firstly, what if it was the user who opened Outlook? I don't think any user will appreciate your code closing Outlook. You might want to check that both Application.Explorers.Count and Application.Inspectors.Count are zero before closing Outlook. Newest version of Outlook will automatically exist if there are no open explorers or inspectors even if you hold a reference to an Outlook object - that was done to guard ageist misbehaving apps that leaked references. To prevent Outlook from closing, hodl a reference to an Explorer object t(even if you do not show it) - call Namespace.getDefaultFolder(olFolderInbox), then hold a reference to the object returned by MAPIFolder.GetExplorer .

Secondly, after calling Send, use Namespace,SyncObjects collection to retrieve the very first SyncObject. Hook up the SyncObject.SyncEnd event and call SyncObject.Start. When SyncEnd event fires, you will be all done. But I don't think you can work with events in VB script...

Upvotes: 0

Bk_
Bk_

Reputation: 99

Try this:

Option Explicit
Sub SendMail()
  Dim outobj, mailobj
  Set outobj = CreateObject("Outlook.Application")

  Set mailobj = outobj.CreateItem(0)

    With mailobj
        .To = "[email protected]"
        .Subject = "Testmail"
        .Body = "If you got this email, my VBs test worked!"
        .Send
    End With

    'Clear the memory
    Set outobj = Nothing
    Set mailobj = Nothing
End Sub
SendMail()
Msgbox("Done")

Upvotes: 0

Bond
Bond

Reputation: 16311

MailItem has a Sent property that indicates when the message has been sent. Try this:

...
objMail.Send

Do Until objMail.Sent
    WScript.Sleep 500
Loop

' Safe to close...
ObjOutlook.Quit

Upvotes: 1

Related Questions