Force open Outlook (1 Viewer)

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
Hi All,

how do i force outlook to open before a forms sends an email?
 

Ranman256

Well-known member
Local time
Yesterday, 23:43
Joined
Apr 9, 2015
Messages
4,339
your path may be different, but like:

shell "C:\Program Files (x86)\Microsoft Office\root\Office16\outlook.exe"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:43
Joined
May 7, 2009
Messages
19,231
you can get/create an instance of Outlook.Application
and set its Object's Visible property to True.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:43
Joined
Feb 28, 2001
Messages
27,140
To do this right, using Ranman256's suggestion, you can find the correct path if you have an icon for Outlook on your desktop. Right-click the icon to see its properties. In the Shortcut tab look at the target. That is the correct path for your machine based on the way you actually installed it. (Or on however someone installed it if not you.)
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
is it possible to open outlook only if it is not already open with above recommendations?
all i did was to add #Shell "Outlook.exe"# in my startup screen
it works fine but it opens another session of outlook if i already have it open
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:43
Joined
May 7, 2009
Messages
19,231
Code:
on vba:

Dim outlookAPP as Object
On Error Resume Next
'get an Instance of outlook, if already open
Set outlookApp = GetObject(, "Outlook.Application")
'if there is an error, then Outlook is not yet Open.
If Err.Number <> 0 Then
    'create New Instance
    Set outlookApp = CreateObject("Outlook.Application")
End If
Err.Clear
On Error Goto 0
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
Code:
on vba:

Dim outlookAPP as Object
On Error Resume Next
'get an Instance of outlook, if already open
Set outlookApp = GetObject(, "Outlook.Application")
'if there is an error, then Outlook is not yet Open.
If Err.Number <> 0 Then
    'create New Instance
    Set outlookApp = CreateObject("Outlook.Application")
End If
Err.Clear
On Error Goto 0
i copied your code into a module and then let the module run with the opening screen
all it does is open the module, it does not give errors but it doesn't open outlook either
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:43
Joined
May 7, 2009
Messages
19,231
you also need to put the code in a Public Sub/Function.
then call it in autoexec macro or in form.
Code:
Public Function GetOutlook() As Object
Static ol As Object
On Error Resume Next
If ol Is Nothing Then
    Set ol = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Set ol = CreateObject("Outlook.Application")
    End If
End If
Err.Clear
On Error GoTo 0
Set GetOutlook = ol
End Function


Public Function ShowOutlook(ol As Object)
    Dim ns, fldr
    Const oFolderInbox As Integer = 6
    Set ns = ol.GetNamespace("MAPI")
    Set fldr = ns.GetDefaultFolder(oFolderInbox)
    fldr.display
End Function


' just to test this
Private Sub test()
Dim o As Object
Set o = GetOutlook()
ShowOutlook o
End Sub
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
you also need to put the code in a Public Sub/Function.
then call it in autoexec macro or in form.
Code:
Public Function GetOutlook() As Object
Static ol As Object
On Error Resume Next
If ol Is Nothing Then
    Set ol = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Set ol = CreateObject("Outlook.Application")
    End If
End If
Err.Clear
On Error GoTo 0
Set GetOutlook = ol
End Function


Public Function ShowOutlook(ol As Object)
    Dim ns, fldr
    Const oFolderInbox As Integer = 6
    Set ns = ol.GetNamespace("MAPI")
    Set fldr = ns.GetDefaultFolder(oFolderInbox)
    fldr.display
End Function


' just to test this
Private Sub test()
Dim o As Object
Set o = GetOutlook()
ShowOutlook o
End Sub
i am not sure how to put this in a public function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:43
Joined
May 7, 2009
Messages
19,231
press Alt-F11 (vba)
on Menu, Insert->Module
paste the code.
Ctrl-S to save.
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
press Alt-F11 (vba)
on Menu, Insert->Module
paste the code.
Ctrl-S to save.
I did that, still when I run the Module, all it does is open VB, it does not open Outlook
 

Isaac

Lifelong Learner
Local time
Yesterday, 20:43
Joined
Mar 14, 2017
Messages
8,774
GetObject for office apps is very glitchy to me, or has been depending on the pc's. (just my personal experience). I would recommend simply using CreateObject - then do your thing, then close the app you opened. Will work every time without a function.
Why don't you post your current code, what you are using to send the object?
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
GetObject for office apps is very glitchy to me, or has been depending on the pc's. (just my personal experience). I would recommend simply using CreateObject - then do your thing, then close the app you opened. Will work every time without a function.
Why don't you post your current code, what you are using to send the object?
I dont have any other code except the one arnelgp supplied in post #8
 

Isaac

Lifelong Learner
Local time
Yesterday, 20:43
Joined
Mar 14, 2017
Messages
8,774
before a forms sends an email?
You don't have a current method that you're using for the form to send an email?

Anyway, you could just try:
Code:
dim objOutlook as object
set objOutlook = createobject("Outlook.Application")
...your code to send the mail continues here.

I haven't automated outlook using my current laptop at my current company, so I just tried it - it totally failed. Your mileage may vary according to your machine's security settings. Randaman's Shell is another possibility that may work for you. In that case you would need to use GetObject afterwards.
 

Gasman

Enthusiastic Amateur
Local time
Today, 04:43
Joined
Sep 21, 2011
Messages
14,231
I posted this in another thread, and it works for me whether I have Outlook open (the norm) and even if I do not.

You can see that I tried the 'is it open method', now all commneted out.

Code:
    ' See if Outlook is open, otherwise open it
    'If fIsOutlookRunning = False Then
    Set objOutlook = CreateObject("Outlook.Application")
    'Call OpenOutlook
    'Pause (5)
    ' Else
    'Set objOutlook = GetObject(, "Outlook.Application")
    'End If
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
I posted this in another thread, and it works for me whether I have Outlook open (the norm) and even if I do not.

You can see that I tried the 'is it open method', now all commneted out.

Code:
    ' See if Outlook is open, otherwise open it
    'If fIsOutlookRunning = False Then
    Set objOutlook = CreateObject("Outlook.Application")
    'Call OpenOutlook
    'Pause (5)
    ' Else
    'Set objOutlook = GetObject(, "Outlook.Application")
    'End If
fIsOutlookRunning gives a invalid outside procedure

#
' See if Outlook is open, otherwise open it
If fIsOutlookRunning = False Then
Set objOutlook = CreateObject("Outlook.Application")
Call OpenOutlook
Pause (5)
Else
Set objOutlook = GetObject(, "Outlook.Application")
End If#
 

Gasman

Enthusiastic Amateur
Local time
Today, 04:43
Joined
Sep 21, 2011
Messages
14,231
fIsOutlookRunning gives a invalid outside procedure

#
' See if Outlook is open, otherwise open it
If fIsOutlookRunning = False Then
Set objOutlook = CreateObject("Outlook.Application")
Call OpenOutlook
Pause (5)
Else
Set objOutlook = GetObject(, "Outlook.Application")
End If#
Well mine is commented out? as can be seen by the code I copied and pasted? :( and it was only dong the same thing arnelgp's code was doing?
Geez, I even mentioned that in the post.!

You really need to start looking at what any code is doing?. All those lines were commented out for a reason, and all you have done is uncomment them?
As you do not have the functions being called, those lines are always going to fail? :(
 

Gismo

Registered User.
Local time
Today, 06:43
Joined
Jun 12, 2017
Messages
1,298
Well mine is commented out? as can be seen by the code I copied and pasted? :( and it was only dong the same thing arnelgp's code was doing?
Geez, I even mentioned that in the post.!

You really need to start looking at what any code is doing?. All those lines were commented out for a reason, and all you have done is uncomment them?
As you do not have the functions being called, those lines are always going to fail? :(
I am not sure how to call the function, could you please assist?
 

Gasman

Enthusiastic Amateur
Local time
Today, 04:43
Joined
Sep 21, 2011
Messages
14,231
There is no function!, other than the CreateObject() , that is the whole point of my post.? Certainly, no home made function.

Just use
Code:
Set objOutlook = CreateObject("Outlook.Application")
but use whatever name your outlook object will be called.

The only drawback I see to my way is that the email will not be sent until outlook is opened again and a Send & Receive is initiated IF Outlook is not open in the first place? As I have mine open ALL the time (it is opened on startup) that does not concern me. If It gets sent on the next open of Outlook, that does not bother me either.

If you are that concerned, arnelgp has given you code to cater whether it is open or not.
I must have changed my code from that logic for some reason, but my memory is so bad, I would never know why now. :D
 

Isaac

Lifelong Learner
Local time
Yesterday, 20:43
Joined
Mar 14, 2017
Messages
8,774
The only drawback I see to my way is that the email will not be sent until outlook is opened again and a Send & Receive is initiated IF Outlook is not open in the first place?
I'm a bit curious on why you believe this is the case. Given a few seconds of time to be open, why would it not send the email, once the appropriate code to send it has fired?
 

Users who are viewing this thread

Top Bottom