Connecting Tech Pros Worldwide Help | Site Map

Basic Outlook Automation Question

deko
Guest
 
Posts: n/a
#1: Nov 12 '05
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


Jeffrey R. Bailey
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Basic Outlook Automation Question


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" <dje422@hotmail.com> wrote in message
news:O2jRb.18313$%v3.1298@newssvr25.news.prodigy.c om...[color=blue]
> 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[/color]
sub[color=blue]
> 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
>
>
>[/color]


deko
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Basic Outlook Automation Question


> Dim ol As Outlook.Application[color=blue]
> 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")[/color]


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


Jeffrey R. Bailey
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Basic Outlook Automation Question


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" <dje422@hotmail.com> wrote in message
news:S_zRb.16808$mr.3667@newssvr27.news.prodigy.co m...[color=blue][color=green]
> > 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")[/color]
>
>
> 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
>
>
>[/color]


Closed Thread


Similar Microsoft Access / VBA bytes