vicky vent
vicky vent

Reputation: 59

How to open outlook with VBA

I like to open Outlook with VBA. It should check if outlook is open and if not then it should open it. I have code but its to big and some times dont work with other macros with Call function. What should be the simple and short code to do this and work with all versions?

#Const LateBind = True

Const olMinimized As Long = 1
Const olMaximized As Long = 2
Const olFolderInbox As Long = 6

#If LateBind Then

Public Function OutlookApp( _
    Optional WindowState As Long = olMinimized, _
    Optional ReleaseIt As Boolean = False _
    ) As Object
    Static o As Object
#Else
Public Function OutlookApp( _
    Optional WindowState As outlook.OlWindowState = olMinimized, _
    Optional ReleaseIt As Boolean _
) As outlook.Application
    Static o As outlook.Application
#End If
On Error GoTo ErrHandler

    Select Case True
        Case o Is Nothing, Len(o.Name) = 0
            Set o = GetObject(, "Outlook.Application")
            If o.Explorers.Count = 0 Then
InitOutlook:
                'Open inbox to prevent errors with security prompts
                o.session.GetDefaultFolder(olFolderInbox).Display
                o.ActiveExplorer.WindowState = WindowState
            End If
        Case ReleaseIt
            Set o = Nothing
    End Select
    Set OutlookApp = o

ExitProc:
    Exit Function
ErrHandler:
    Select Case Err.Number
        Case -2147352567
            'User cancelled setup, silently exit
            Set o = Nothing
        Case 429, 462
            Set o = GetOutlookApp()
            If o Is Nothing Then
                Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
            Else
                Resume InitOutlook
            End If
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error"
    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("Outlook.Application")

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 open_outlook()
    Dim OutApp  As Object
    Set OutApp = OutlookApp()
    'Automate OutApp as desired
End Sub

Upvotes: 3

Views: 43784

Answers (4)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

See How to automate Outlook from another program for the sample code. You can also use the GetObject method for getting the running instance of Outlook instead of creating a new one:

Set objOutlook = GetObject(, "Outlook.Application")

However, Outlook is a singleton. Each time you call the CreateObject method you will get the same instance. You can't run two instances of Outlook at the same time. See GetObject in Word VBA script to find Outlook instance fails with 429 error unless both apps running as administrator for more info.

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

Upvotes: 3

Moreno
Moreno

Reputation: 638

You could use something simplier:

    Sub EmailMe()
    
    dim mail as object
    dim msg as object
    
    set mail= createobject("Outlook.Application")
    set msg=mail.createitem(0)
    
     with msg
      .to="[email protected];...."
      .subject="What are you sending this for"  
      .body="Whatever you want to say"
      .attachments.add Activeworkbook.fullname
      .send
     end with
    
    end sub
      

Upvotes: 1

Orin Moyer
Orin Moyer

Reputation: 509

Dim oOutlook As Object

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOutlook Is Nothing Then
    Shell ("OUTLOOK")
Else
    'already open
End If

Upvotes: 1

Atul Vij
Atul Vij

Reputation: 241

I think you can try below code.Its shortest code i tried to open in my all VBA coding.

Sub Open_Outlook()

Shell ("OUTLOOK")

End Sub

Upvotes: 6

Related Questions