Solved

OUTLOOK AUTOMATIC OPENING

Posted on 2007-04-02
28
277 Views
Last Modified: 2012-05-05
Hello,

I have incorporated some code into a spreadsheet that when initiated is supposed to check whether Outlook is already open and if not then open it. But nothing seems to be happening. Could someone take a look?

Thanks.

Private Sub Command1_Click()
   Dim OlApp As Outlook.Application
   
   On Error Resume Next
   Set OlApp = GetObject(, "Outlook.Application")
   If Err.Number = 429 Then               'Outlook not running, so start it
      MsgBox "Starting Outlook now..."
      Set OlApp = New Outlook.Application
   End If
   
   Set OlApp = Nothing
End Sub
0
Comment
Question by:f19l
  • 10
  • 8
  • 7
  • +1
28 Comments
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
open outlook
and check return value of this > GetObject(, "Outlook.Application")
this seems correct!!
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
because OlApp is a local var to that function only
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
your code seems perfactly ok to me as this sample:
but have u added the reference to the Microsoft Outlook Object library?

Private Sub Command1_Click()
   Dim OlApp As Outlook.Application
   
   On Error Resume Next
   Set OlApp = GetObject(, "Outlook.Application")
   If Err.Number = 429 Then               'Outlook not running, so start it
      MsgBox "Starting Outlook now..."
      Set OlApp = New Outlook.Application
   End If
   
   Set OlApp = Nothing
End Sub
0
 

Author Comment

by:f19l
Comment Utility
When I run the code and step through it everything seems to be fine but Outlook just does not open.
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
here you are creating object !! you are not opening it
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
few other ways:-

 Dim Olook As Outlook.Application
Set Olook = CreateObject("Outlook.Application")
Olook.Application.Visible = True


http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan07/hey0124.mspx
0
 

Author Comment

by:f19l
Comment Utility
The first method has a run-time error '438': Object doed not support this property or method.
The second has a run-tmie error '-2417221231 (80040111)': Automation error ClassFactory cannot supply requested class.
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
I use this function

Function OutlookRunning() As Boolean
On Error Resume Next
Set outlApp = GetObject(, "Outlook.Application")
If outlApp Is Nothing Then ' get a new one
    OutlookRunning = False
Else
    OutlookRunning = True
End If
End Function


Called/Used this way

    Dim OutMail As Outlook.MailItem
    If Not OutlookRunning Then
    Set OutApp = New Outlook.Application
    Else
    Set OutApp = Outlook.Application
    End If
0
 

Author Comment

by:f19l
Comment Utility
It still does not work. I can run through the code but Outlook does not open.
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
what does the rest of your code look like?
0
 

Author Comment

by:f19l
Comment Utility
Function OutlookRunning() As Boolean
On Error Resume Next
Set outlApp = GetObject(, "Outlook.Application")
If outlApp Is Nothing Then ' get a new one
    OutlookRunning = False
Else
    OutlookRunning = True
End If
End Function

Sub command5()


Dim OutMail As Outlook.MailItem
    If Not OutlookRunning Then
    Set OutApp = New Outlook.Application
    Else
    Set OutApp = Outlook.Application
    End If
End Sub
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
Here is an example of how Outlook Automation works. It doesn't open with the code you're using (which appears to be what I gave you...) because you're not doing anything with it.

If you go into your windows task manager, outlook probably IS running, you've just not opened a mail item to see it.

Here is a working code sample.

Function SendMessages()
'On Error GoTo Error_Routine
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim strAttach As String
    strAttach = "C:\TempSnp.snp"                      ' This is your Temp Output File

'-------------------------------This will output your file as snapshot-----------------
DoCmd.OutputTo acReport, "Control2", "SnapshotFormat(*.snp)", "C:\TempSnp.snp", False, ""
'--------------------------------------------------------------------------------------
    If Not OutlookRunning Then
    Set OutApp = New Outlook.Application
    Else
    Set OutApp = Outlook.Application
    End If
    Set OutMail = OutApp.CreateItem(olMailItem)
    With OutMail
        .To = "Bob@test.com"                        ' This is your e-mail TO address
        '.Subject = Me.Text277.Value                    ' This is your Subject Line from your form
        .Body = "Faults Report"
            If Not IsMissing(strAttach) Then
        .Attachments.Add (strAttach)                   ' This is the attachment
            End If
        .Display                                       ' Change to Send if you don't want to view the e-mail
   End With
   Set OutMail = Nothing
   Set OutApp = Nothing
End Function
0
 

Author Comment

by:f19l
Comment Utility
What about trying to use a vbs file that checks whether Outlook is open and if it is not then opens it. This file could then be run as part of an automatic scheduler.
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
Are all you trying to do is open outlook? Not create an e-mail automatically?

Shell "OUTLOOK.EXE", vbMaximizedFocus   'Automatically opens Outlook
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Can you use WMI?

Dim objWMI:Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objOL = objWMI.ExecQuery("Select * From Win32_Process Where Name='Outlook.exe'")
If objOL.Count < 1 Then
  wscript.echo "Outlook is NOT running"
End If
Set objWMI=Nothing
0
 

Author Comment

by:f19l
Comment Utility
What I would like is some code that will simply check whether Outlook is open or not and if it is then open it. No emails need to be fired off at this stage. Obviously it will take some time for received emails to start appearing in my inbox. I have another process that basically involves VB code checking through the inbox to see if certain files have arrived. Therefore I think that the best process is to use a vbs file as part of an automatic scheduler that checks and if necessary opens up Outlook some time before the second mentioned process is initiated.
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
try the shell command...it will open the application if it is not open, and will open the app if it isn't open. Are you finding that more than one instance of Outlook is being opened or something?
0
 

Author Comment

by:f19l
Comment Utility
The above mentioned processes are meant to happen first thing in the morning before I get into work. Occasionally IT may do some upgrade work on my PC which means that applications like Outlook and Excel may have been shut doen, which is why I need to have a check in place.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
Comment Utility
So, use this...

Dim objWMI:Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim objShell: Set objShell=CreateObject("Wscript.Shell")
Set objOL = objWMI.ExecQuery("Select * From Win32_Process Where Name='Outlook.exe'")
If objOL.Count < 1 Then objShell.Run "Outlook.exe"  
Set objWMI=Nothing
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
>>which means that applications like Outlook and Excel may have been shut doen, which is why I need to have a check in place.

if you're running this from Excel...what if it is shut down also? I guess I'm confused. You can set up a task to open an application through your scheduler...but even that, if your staff has done upates to your computer, chances are they've logged you off...so all bets are off?

I don't mean to confuse this...just trying to wrap my brain around the shortcomings of it.
J
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
oh !! was not expecting this to last too long.
now let me go thru" the discussion

this will help you further...
www.outlookcode.com
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
you should have a look here:
http://www.microeye.com/resources/ObjectModel.htm

please see OlApp's properties in debug mode..
0
 

Author Comment

by:f19l
Comment Utility
Experts,

Right now I am setting up the following process:-

First thing in the morning before I get into work I receive emails in various formats that need to be saved in various folders. I currently have a vbs file that at 7am every weekday is fired off via an automatic scheduler that opens up an excel spreadsheet, bypassing the various messages that appear, and then fires off additional macros that are desgined to extract the email attachments and save them in the relevant folders. If for whatever reason Outlook is shut down then this will obviously cause problems which is why I asked the question about opening it. If work is done on my PC by IT then certainly there is a problem in that my PC could be locked. This is being investigated but if you are aware of a way in which locking will not have any effect on the process just described then please let me know.
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
Sub CheckOutlook()
On Error Resume Next
Set outlApp = GetObject(, "Outlook.Application")
If outlApp Is Nothing Then
    Shell "OUTLOOK.EXE", vbMaximizedFocus   'Automatically opens Outlook
End If
End Function


give that a try
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
sorry, small correction

Sub CheckOutlook()
On Error Resume Next
Set outlApp = GetObject(, "Outlook.Application")
If outlApp Is Nothing Then
    Shell "OUTLOOK.EXE", vbMaximizedFocus   'Automatically opens Outlook
End If
End Sub
0
 

Author Comment

by:f19l
Comment Utility
Experts,

I have looked at all the code provided and the best option for me is to use sirbounty's stand-alone code as part of an auto scheduler. If, however anyone is aware of a way to get around the problem of opening Excel using a vbs file even when the PC is locked then let me know.

Thanks for your help.
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Happy to help. Thanx for the grade! :^)
0
 

Author Comment

by:f19l
Comment Utility
No problem. I have another problem that you might be able to help with. Look out for a problem called TEXTFILE-MONTHEND.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now