By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,500 Members | 830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,500 IT Pros & Developers. It's quick & easy.

Basic Outlook Automation Question

P: n/a
When adding Outlook Appointment Items from Access, should I use:

Set ol = New Outlook.Application
--or--
Set ol = CreateObject("Outlook.Application")

Outlook seems to crash either way.
The Outlook application (and/or Calendar) may or may not be open when this sub
runs.

Here is code:

Private Sub AddAppt_Click()

Dim ol As Outlook.Application
Dim ola As Outlook.AppointmentItem

Set ol = New Outlook.Application
'--or--
'Set ol = CreateObject("Outlook.Application")

Set ola = ol.CreateItem(olAppointmentItem)
With ola
.Start = Me!SomeDate
.Subject = Me!Subject
.Location = Me!Location
.AllDayEvent = True
.ReminderMinutesBeforeStart = "1440"
.Body = Me!Body
.ReminderSet = True
.Save
.Close (olSave)
End With
End Sub
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Outlook shouldn't crash when you attempt to open it from Access. Try the
code below and see if you still experience a crash. Also check to see how
many times Outlook is running in the background, not closed (use taskmanager
for this if your OS supports it).

Code:

Dim ol As Outlook.Application
Const CANT_CREATE_OBJECT As Integer = 429

'turn off error handling
'test first to see if outlook is open
On Error Resume Next
Set ol = GetObject(Class:="Outlook.Application")
If Err.Number = CANT_CREATE_OBJECT Then
Set ol = CreateObject(Class:="Outlook.Application")
End If
'turn on error handling, if you have any
On Error Goto ERR_TRAP
'rest of your code past here

This method relies on the error raised when you try the "GetObject"
function. If you go to the website at http://www.mvps.org/access/ you can
find a function under the "APIs/Find Out if an Application is Currently
Running" link that will allow you to test for various office applications
including Outlook, providing a different method of achieving the same
result. Also there is "Close Another Application" which you will find useful
for this type of problem. I have rolled my own versions of these in the
past, but the ones on this site are just as good or better. There are many
good examples on the site and I highly recommend adding it to your
"Favorites".

Jeffrey R. Bailey
"deko" <dj****@hotmail.com> wrote in message
news:O2******************@newssvr25.news.prodigy.c om...
When adding Outlook Appointment Items from Access, should I use:

Set ol = New Outlook.Application
--or--
Set ol = CreateObject("Outlook.Application")

Outlook seems to crash either way.
The Outlook application (and/or Calendar) may or may not be open when this sub runs.

Here is code:

Private Sub AddAppt_Click()

Dim ol As Outlook.Application
Dim ola As Outlook.AppointmentItem

Set ol = New Outlook.Application
'--or--
'Set ol = CreateObject("Outlook.Application")

Set ola = ol.CreateItem(olAppointmentItem)
With ola
.Start = Me!SomeDate
.Subject = Me!Subject
.Location = Me!Location
.AllDayEvent = True
.ReminderMinutesBeforeStart = "1440"
.Body = Me!Body
.ReminderSet = True
.Save
.Close (olSave)
End With
End Sub

Nov 12 '05 #2

P: n/a
> Dim ol As Outlook.Application
Const CANT_CREATE_OBJECT As Integer =
On Error Resume Next
Set ol = GetObject(Class:="Outlook.Application")
If Err.Number = CANT_CREATE_OBJECT Then
Set ol = CreateObject(Class:="Outlook.Application")

I'll give it a whirl... but, I'm wondering if these are equivalent:

Set ol = CreateObject("Outlook.Application")
--and--
Set ol = CreateObject(Class:="Outlook.Application")

my guess is that they are
Nov 12 '05 #3

P: n/a
This is a couple of days late, but hopefully you will see it.

Yes, the two statements are equivalent in terms of execution and your code
did not contain a syntax error. I don't think I suggested that it did, but
if I did I apologize. The difference between the two statements is a
difference of style. I try as much as possible to follow a style of coding
the insures the maximum clarity for anyone reading my code (including myself
six months in the future when I may not be terribly familiar with the
problem that I had solved previously). As a result, I use the assignment
operator ":=" and named arguments.

Having said that, the fragment I posted was not complete. Within the "if
"statement the line:
Err.Clear
should be added to clear the handled error from the error object.

The code I posted was only an attempt to keep you from opening multiple
copies of Outlook, which I have never found to be good. I thought having
multiple copies open the background might be the problem with the crashing.

If you have discovered what was causing the crash you should post back to
the group so that your experience can help others in the future.
--
Jeffrey R. Bailey
"deko" <dj****@hotmail.com> wrote in message
news:S_*****************@newssvr27.news.prodigy.co m...
Dim ol As Outlook.Application
Const CANT_CREATE_OBJECT As Integer =
On Error Resume Next
Set ol = GetObject(Class:="Outlook.Application")
If Err.Number = CANT_CREATE_OBJECT Then
Set ol = CreateObject(Class:="Outlook.Application")

I'll give it a whirl... but, I'm wondering if these are equivalent:

Set ol = CreateObject("Outlook.Application")
--and--
Set ol = CreateObject(Class:="Outlook.Application")

my guess is that they are

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.