Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use VBA to check if Outlook is loaded

8 views
Skip to first unread message

DFStoneJr

unread,
Dec 11, 2002, 11:15:36 PM12/11/02
to
Can I use VBA to determine whether Outlook is loaded, and, if not to do so?
If so, what would the code be? (Win2K, Office2K)

TIA


Tom Ogilvy

unread,
Dec 11, 2002, 11:38:39 PM12/11/02
to
Sub TestforOutlook()
Dim objOutlook As Object
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If objOutlook Is Nothing Then
MsgBox "Outlook not running"
Else
MsgBox "Outlook is running"
End If

End Sub


Regards,
Tom Ogilvy

DFStoneJr <DFSt...@NOSPAM.yahoo.com> wrote in message
news:uvg3b9f...@corp.supernews.com...

DFStoneJr

unread,
Dec 12, 2002, 9:13:12 AM12/12/02
to
Tom,
Thanks. This code tests to see whether Outlook is running and returns
a message telling whether it is or not. Can it be modified to load
Outlook if it's not running?

TIA.

"Tom Ogilvy" <twog...@msn.com> wrote in message news:<OZ4UBgZoCHA.2224@TK2MSFTNGP11>...

Tom Ogilvy

unread,
Dec 12, 2002, 9:21:49 PM12/12/02
to
Sub TestforOutlook()
Dim objOutlook As Object
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If objOutlook Is Nothing Then

set objOutlook = CreateObject("Outlook.Application")
End If

' objOutlook should now hold a reference to a running instance of Outlook
End Sub

Untested, but should work.

Regards,
Tom Ogilvy

DFStoneJr <DFSt...@yahoo.com> wrote in message
news:26cf567e.02121...@posting.google.com...

DFStoneJr

unread,
Dec 13, 2002, 9:31:29 AM12/13/02
to
Tom, either the code doesn't work, you misunderstood my question, or I
didn't phrase it properly.

I'd like for the code to first test whether Outlook is running. Based
on the first response you gave, which returned a message indicating
Outlook's status, it does that. But I also want th code to run
Outlook if the result of the test indicates that Outlook is not
running. In the second response you provide, I can see where the line
"set objOutlook = CreateObject("Outlook.Application")" sets the
variable objOutlook, but I can't see that anything is doen with the
variable once it's set. What am I missing?

Thanks for your help and patience.


"Tom Ogilvy" <twog...@msn.com> wrote in message news:<eZiMM4koCHA.2636@TK2MSFTNGP10>...

Tom Ogilvy

unread,
Dec 13, 2002, 3:43:08 PM12/13/02
to
Since it doesn't do anything with outlook, it closes immediately I would
think. Here is some code that does something.

Sub TestforOutlook()
'Dim objOutlook As Outlook.Application
'Dim objOutlookMsg As Outlook.MailItem
'Dim objOutlookRecip As Outlook.Recipient
'Dim objOutlookAttach As Outlook.Attachment
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
Dim DisplayMsg As Boolean
Dim AttachmentPath As String
AttachmentPath = "C:\Data\message.txt"

DisplayMsg = True


On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If objOutlook Is Nothing Then

Set objOutlook = CreateObject("Outlook.Application")

End If

' Create the message.

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Ogilvy, Thomas")
objOutlookRecip.Type = 1 'olTo

' Add the CC recipient(s) to the message.
'Set objOutlookRecip = .Recipients.Add("Michael Suyama")
'objOutlookRecip.Type = 2 'olCC

' Add the BCC recipient(s) to the message.

' Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
'objOutlookRecip.Type = 3 'olBCC

' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "This is the body of the message." & vbCrLf & vbCrLf
.Importance = 2 'olImportanceHigh 'High importance

' Add attachments to the message.

Set objOutlookAttach = .Attachments.Add(AttachmentPath)

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Send
End If

End With
set objOutlookMsg = nothing
set objOutlookRecip = nothing
set objOutlookAttach = nothing
Set objOutlook = Nothing
End Sub


Regards,
Tom Ogilvy

"DFStoneJr" <DFSt...@yahoo.com> wrote in message

0 new messages