OUTLOOK AUTOMATIC OPENING

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
f19lAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theGhost_k8Database ConsultantCommented:
open outlook
and check return value of this > GetObject(, "Outlook.Application")
this seems correct!!
0
theGhost_k8Database ConsultantCommented:
because OlApp is a local var to that function only
0
theGhost_k8Database ConsultantCommented:
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

f19lAuthor Commented:
When I run the code and step through it everything seems to be fine but Outlook just does not open.
0
theGhost_k8Database ConsultantCommented:
here you are creating object !! you are not opening it
0
theGhost_k8Database ConsultantCommented:
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
f19lAuthor Commented:
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
jefftwilleyCommented:
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
f19lAuthor Commented:
It still does not work. I can run through the code but Outlook does not open.
0
jefftwilleyCommented:
what does the rest of your code look like?
0
f19lAuthor Commented:
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
jefftwilleyCommented:
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
f19lAuthor Commented:
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
jefftwilleyCommented:
Are all you trying to do is open outlook? Not create an e-mail automatically?

Shell "OUTLOOK.EXE", vbMaximizedFocus   'Automatically opens Outlook
0
sirbountyCommented:
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
f19lAuthor Commented:
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
jefftwilleyCommented:
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
f19lAuthor Commented:
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
sirbountyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jefftwilleyCommented:
>>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
theGhost_k8Database ConsultantCommented:
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
theGhost_k8Database ConsultantCommented:
you should have a look here:
http://www.microeye.com/resources/ObjectModel.htm

please see OlApp's properties in debug mode..
0
f19lAuthor Commented:
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
jefftwilleyCommented:
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
jefftwilleyCommented:
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
f19lAuthor Commented:
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
sirbountyCommented:
Happy to help. Thanx for the grade! :^)
0
f19lAuthor Commented:
No problem. I have another problem that you might be able to help with. Look out for a problem called TEXTFILE-MONTHEND.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.