• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Restart Outlook from VBA it is closed

ThrottleWorks

Excel Ninja
Hi,

I am using below mentioned code to create e-mails.
Sometimes, while running the code, Outlook get closed thus results in error.
Code:
Option Explicit
Sub FlodAutomation()
    Dim SubjectLine As Variant
    Dim FromEmail As Variant
    Dim ToEmail As Variant
    Dim CcEmail As Variant
    Dim OutApp As Variant
    Dim OutMail As Variant
    Dim vInspector As Variant
    Dim wEditor As Variant
    Dim TempLr As Long
           
    Dim FlodSht As Worksheet
    Set FlodSht = ThisWorkbook.Worksheets("Email Sheet")
   
    FromEmail = ""
    ToEmail = FlodSht.Range("C2").Value
    CcEmail = FlodSht.Range("C3").Value
   
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
   
    With OutMail
        .SentOnBehalfOfName = FromEmail
        .To = ToEmail
        .CC = CcEmail
        .Subject = ThisWorkbook.Worksheets("Mapping").Range("A4").Value
        .Body = ""
       
        '.Attachments.Add (ThisWorkbook.Worksheets("Macro").Range("P4").Value)
       
        TempLr = FlodSht.Cells(EmailTemplate.Rows.Count, 8).End(xlUp).Row
        FlodSht.Range("H1:P" & TempLr).Copy
        Set vInspector = OutMail.GetInspector
        Set wEditor = vInspector.WordEditor
       
        wEditor.Application.Selection.Start = Len(.Body)
        wEditor.Application.Selection.End = wEditor.Application.Selection.Start
       
        .Display
        wEditor.Application.Selection.Paste
    End With
    Application.CutCopyMode = 1
End Sub

Can anyone please tell me how do I open Outlook with VBA (if it is close).
I tried few Google options but it is not working for me.
 
Is CreateObject method in your code giving error?

I am not 100% sure what is the issue but you can try to use function like below to test if outlook is running (you can keep it in the code if it is not checked repetitively)
Code:
Public Sub TestOutlook()
Dim oApp As Object
'// Example code for testing function
If IsOutLookRunning Then
    MsgBox "Outlook is running!", vbInformation
Else
    MsgBox "Outlook is not running!", vbExclamation
    Set oApp = CreateObject("Outlook.Application")
End If
End Sub
Private Function IsOutLookRunning() As Boolean
Dim oApp As Object
On Error Resume Next
    Set oApp = GetObject(, "Outlook.Application")
    If Not oApp Is Nothing Then
        IsOutLookRunning = True
    End If
On Error GoTo 0
End Function
 
Hi @Debaser , thanks for the help. :)

Hi @shrivallabha , thanks for the help. May be am facing system issue.
I closed Outlook on my system and run the code provided you.

Am getting MsgBox "Outlook is not running!" but Outlook is not created after this line. There is no bug, macro runs till the end.

In fact, I faced same issue with Google codes also.
Sometimes, Outlook gets opened, sometimes it does not.

Pretty confusing for me.

Good night. :)
 
If the code is running without halting means VBA is able to create an instance of outlook application when it runs.

If you step through the code (using F8) and execute this line
Code:
Set oApp = CreateObject("Outlook.Application")
and verify in the task manager, OUTLOOK.exe should be visible in the process list. Possibly it closes down at the end of code execution and doesn't keep it running as it would be if it were started manually.

Also see, if the code posted in below link is of any help:
http://www.rondebruin.nl/win/s1/outlook/openclose.htm
 
Back
Top