VBA code to Open Outlook

sdohertyccb

Board Regular
Joined
Feb 15, 2005
Messages
91
I have code that sends an email through VBA code in excel. I want to test to see if Outlook is open (not everone will be using this on their primary email box) and if not, to open then send the attachments.
I have simpliefied the code below, would appreciate it if someone can give me the additional code to open this application.
When I run the following code on a box that does not have Oulook open, I get into some kind of a loop with a message that pops up that says "Excel is waiting for another OLE application to finish", which it never does...
Thoughts?
Thanks so much, in advance.

Code:
Sub send_test()
Dim Outlook_App As Object
Dim Outlook_Mail As Object

Set Outlook_App = CreateObject("Outlook.Application")
Set Outlook_Mail = Outlook_App.CreateItem(0)
On Error Resume Next
With Outlook_Mail
    .To = "me@xxxx.com"
    .Subject = "Test Email"
    .Body = "Help me!"
    .Send
End With
On Error GoTo 0

Set Outlook_Mail = Nothing
Set Outlook_App = Nothing
    
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
try this see if it helps

Code:
Sub check_is_running()
Dim Process As Object
Dim proc_name As String
Dim bl  As Boolean
proc_name = "Outlook.exe" '\change process name here
bl = False
    For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & proc_name & "'")
        If UCase(Process.Name) = UCase(proc_name) Then
            bl = True
            Exit For
        End If
    Next Process
    
    If bl = False Then
        MsgBox "Outlook is not open"
    Else
        MsgBox "Outlook is open"
    End If
End Sub
 
Upvote 0
Ashish, thanks for the assistance.
That works to establish whehter the program is running, now, do you know what the command to open Outlook is so I can send an email?
 
Upvote 0
thanks jd.
I have tried .Display.
It displays the email, but does not open Outlook to allow the email to be sent.
That is how I got into the loop that says "Excel is waiting for anothe OLE application to finish"
Any other thoughts?
 
Upvote 0
By the way, Isn't the CreateObject Function supposed to start the application? Or does it just start a new email from an alreadyu open application?
 
Upvote 0
try this

Public Sub OpenOutlook()
Dim ret As Long
On Error GoTo aa
ret = ShellExecute(Application.hwnd, vbNullString, "Outlook", vbNullString, "C:\", SW_SHOWNORMAL)
If ret < 3 Then

MsgBox "Outlook is not found.", vbCritical, "SN's Customised Solutions"
End If
aa:
End Sub
 
Upvote 0
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Put the above at the top of the module or form code where the sub is located
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,245
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top