title: Excel VBA to detect if Outlook is open, if its not ,then open it
body: <p>I have written code to download an attachment to a specified folder.</p>
  <pre><code>Const olFolderInbox = 6
  
  Sub detectpp_plate_record1()
  
  Dim oOutlook As Object
  Dim oOlns As Object
  Dim oOlInb As Object
  Dim unRead, m As Object, att As Object
  
  '~~&gt; Get Outlook instance
  Set oOutlook = GetObject(, &quot;Outlook.application&quot;)
  Set oOlns = oOutlook.GetNamespace(&quot;MAPI&quot;)
  Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
  
  '~~&gt; Check if there are any actual unread emails
  Set unRead = oOlInb.Items.Restrict(&quot;[UnRead] = True&quot;)
  
  ' File_Path = &quot;D:\Attach\&quot;
  
  File_Path = &quot;C:\Users\Desktop\pocket setter excel\&quot;
  
  If unRead.Count = 0 Then
      MsgBox &quot;NO Unread Email In Inbox&quot;
  Else
      For Each m In unRead
          If m.Attachments.Count &gt; 0 Then
              For Each att In m.Attachments
                  If att.Filename Like &quot;plate record*&quot; Then
                      MsgBox &quot;Unread Email with attachment available In Inbox&quot;
                     
                      'Like &quot;plate record*.xls&quot;
                      '~~&gt; Download the attachment
                      ' to the file path and file name
                      'att.Filename = name of attachement
                          
                      att.SaveAsFile File_Path &amp; &quot;plate record&quot;
                              
                      'att.SaveAsFile File_Path &amp; att.Filename
                              
                      '&amp; Format(plate record)
                              
                      ' mark attachment as read               
                      m.unRead = False
                      DoEvents
                      m.Save
                 
                      WorkFile = Dir(File_Path &amp; &quot;*&quot;)
  
                      Do While WorkFile &lt;&gt; &quot;&quot;
  
                         If Right(WorkFile, 4) &lt;&gt; &quot;xlsm&quot; Then
                            Workbooks.Open Filename:=File_Path &amp; WorkFile
                            ActiveWorkbook.SaveAs Filename:= _
                              File_Path &amp; WorkFile &amp; &quot;&quot;, FileFormat:= _
                              xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
                            ActiveWorkbook.Close
                            Kill File_Path &amp; WorkFile
                          End If
  
                          WorkFile = Dir()
                      Loop
  
                      Exit Sub
                  End If
              Next att
          End If
      Next m
  End If
  End Sub
  </code></pre>
  <p><strong>The problem : This can be executed only when Outlook is open.</strong></p>
  <p><strong>Therefore I have to separately open Outlook.</strong></p>
  <p><strong>My requirement is to use Excel VBA code to detect if Outlook is open, if it is not, then it should be opened.</strong></p>
  <p><strong>---------------------UDATE-----------------------</strong></p>
  <p>I combined the above code with the following code.</p>
  <pre><code>#Const LateBind = True
  
  Const olMinimized As Long = 1
  Const olMaximized As Long = 2
  Const olFolderInbox As Long = 6
  
  Sub detectpp_plate_record()
      MyMacroThatUseOutlook
      detectpp_plate_record1
  End Sub
  
  #If LateBind Then
  
  Public Function OutlookApp( _
      Optional WindowState As Long = olMinimized, _
      Optional ReleaseIt As Boolean = False _
      ) As Object
      Static oOutlook As Object
  #Else
  Public Function OutlookApp( _
      Optional WindowState As Outlook.OlWindowState = olMinimized, _
      Optional ReleaseIt As Boolean _
  ) As Outlook.Application
      Static oOutlook As Outlook.Application
  #End If
  On Error GoTo ErrHandler
   
      Select Case True
          Case oOutlook Is Nothing, Len(oOutlook.name) = 0
              Set oOutlook = GetObject(, &quot;Outlook.Application&quot;)
              If oOutlook.Explorers.Count = 0 Then
  InitOutlook:
                  'Open inbox to prevent errors with security prompts
                  oOutlook.Session.GetDefaultFolder(olFolderInbox).Display
                  oOutlook.ActiveExplorer.WindowState = WindowState
              End If
          Case ReleaseIt
              Set oOutlook = Nothing
      End Select
      Set OutlookApp = oOutlook
   
  ExitProc:
      Exit Function
  ErrHandler:
      Select Case Err.Number
          Case -2147352567
              'User cancelled setup, silently exit
              Set oOutlook = Nothing
          Case 429, 462
              Set oOutlook = GetOutlookApp()
              If oOutlook Is Nothing Then
                  Err.Raise 429, &quot;OutlookApp&quot;, &quot;Outlook Application does not appear to be installed.&quot;
              Else
                  Resume InitOutlook
              End If
          Case Else
              MsgBox &quot;Error &quot; &amp; Err.Number &amp; &quot;: &quot; &amp; Err.Description, vbCritical, &quot;Unexpected error&quot;
      End Select
      Resume ExitProc
      Resume
  End Function
  
  #If LateBind Then
  Private Function GetOutlookApp() As Object
  #Else
  Private Function GetOutlookApp() As Outlook.Application
  #End If
  On Error GoTo ErrHandler
      
      Set GetOutlookApp = CreateObject(&quot;Outlook.Application&quot;)
      
  ExitProc:
      Exit Function
  ErrHandler:
      Select Case Err.Number
          Case Else
              'Do not raise any errors
              Set GetOutlookApp = Nothing
      End Select
      Resume ExitProc
      Resume
  End Function
  
  Sub MyMacroThatUseOutlook()
      Dim OutApp  As Object
      Set OutApp = OutlookApp()
      'Automate OutApp as desired
  End Sub
  
   
  </code></pre>
  <p>Now, if Outlook is open the code searches for the specified unread email.</p>
  <p>If Outlook is closed, it opens it, but afterwards there is an error</p>
  <p>Run time error 429:</p>
  <p>ActiveX component cant create object.</p>
  <p>Therefore once again I have to click on button for the code to search for the specified emails.</p>
  <p>How do I get rid of this error and perform this in one go?</p>