Solved

OUTLOOK AUTOMATIC OPENING

Posted on 2007-04-02
28
282 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
ID: 18835283
open outlook
and check return value of this > GetObject(, "Outlook.Application")
this seems correct!!
0
 
LVL 21

Expert Comment

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

Expert Comment

by:theGhost_k8
ID: 18835317
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:f19l
ID: 18835383
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
ID: 18835394
here you are creating object !! you are not opening it
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18835415
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
ID: 18835546
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
ID: 18835564
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
ID: 18835700
It still does not work. I can run through the code but Outlook does not open.
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18835737
what does the rest of your code look like?
0
 

Author Comment

by:f19l
ID: 18835764
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
ID: 18835795
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
ID: 18835993
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
ID: 18836178
Are all you trying to do is open outlook? Not create an e-mail automatically?

Shell "OUTLOOK.EXE", vbMaximizedFocus   'Automatically opens Outlook
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18836182
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
ID: 18836301
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
ID: 18836323
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
ID: 18836326
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
ID: 18836346
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
ID: 18836367
>>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
ID: 18836408
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
ID: 18836418
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
ID: 18836680
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
ID: 18836757
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
ID: 18836772
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
ID: 18836804
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
ID: 18836911
Happy to help. Thanx for the grade! :^)
0
 

Author Comment

by:f19l
ID: 18837328
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 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