Dan - RE LATE BINDING OBJECT REFERENCES

S

S Jackson

Dan:

This is in response to my post below about object libraries (wow, I really
started something there, didn't I?). I am posting up here as I wanted to be
sure you saw this post.

Thanks for your response. I am brand new to VBA for Access and am mostly
working through and learning as I go. I have bought myself a book, but
have not gotten too far yet.
I got the code to use automation from MS Knowledge Base Article 209963.
After working through it and modifying it for my purposes, I think I
understand most of it, but certainly not all of it. Hence, I do not fully
grasp what you advise in your response below about using late binding. I
think I get the general idea: "Don't use object references because not all
users will have the same versions." But, how do I apply the code you gave
me to my code:

Private Sub Command18_Click()
On Error GoTo Add_Err

'Exit procedure if Task already added to Outlook
If Me.AddedToOutlook = True Then
MsgBox "Task is already added to Outlook"

'Add Task

Else
Dim objOutlook As Outlook.Application
Dim objTask As Outlook.TaskItem

Set objOutlook = CreateObject("Outlook.Application")
Set objTask = objOutlook.CreateItem(olTaskItem)

With objTask
.Subject = Me.CaseName & " / DHS No. " & Me.DHSNo
.DueDate = Me.Date
.Body = Me.Event
.ReminderSet = True

.Save
End With

'Release the TaskItem object variable
Set objTask = Nothing

End If

'Release the Outlook object variable
Set objOutlook = Nothing

'Set the AddedToOutlook flag, display a message
Me.AddedToOutlook = True
MsgBox "Task Added!"

Exit Sub
Add_Err:
MsgBox "Error" & Err.Number & vbCrLf & Err.Description
Exit Sub
End Sub


I am going to go fool with my code and see if I can figure this one out in
the meantime.

Thanks!
S. Jackson
 
D

Dan Artuso

Hi,
All you have to do is Dim the variables as Object.

Dim objOutlook As Object
Dim objTask As Object

Any objects that you create yourself, use CreateObject, as you have done.
The TaskItem object is created by your Outlook Application variable so
you don't have to worry about that one.
 
D

Dan Artuso

Hi,
You don't have to do anything special, just use the line as is:
objTask=objOutook.CreateItem(olTaskItem)

Application is a class contained in the Outlook object library.
It has properties and methods (think Subs and Functions)
Once you create the Application object, you can access any of
it's properties or methods, CreateItem of course being one of them.
These things are all internally defined inside the Application object
you created. You just use them. No need to define anything.

The CreateItem function returns a reference to an object that it creates

When you're in the development phase, I recommend keeping your reference
to Outlook so that you get the benefit of Intelisense. That's why I defined
the
constant as oTaskItem instead of olTaskItem.

You still use CreateObject(), but keep the variable declarations as:
Dim objTask AS TaskItem

Then when you're ready, do this:
Dim objTask As Object ' TaskItem

to all declarations and remove the reference.
 
S

S Jackson

First, thank you for your time and patience here. I hope that others
reading this thread will benefit also.

Unfortunately, I still have something wrong because now I am getting this
error:

Error91
Object variable or With block variable not set

I did what you said: Made a reference to the MS Outlook Object library so I
can use Intelisense.
I declared the variables:
Dim objOutlook As Application ' Outlook Application
Dim objTask As TaskItem ' Task Item

Then I tried to set the variables:
Set objOutlook = CreateObject ("Outlook.Application")

BUT: when I attempt to set the objTask variable the "CreateItem" does not
come up on the list that VBA pops up when you type the period after
objOutlook. I want the statement to look like this, right?

Set objTask = objOutlook.CreateItem(oTaskItem)

What is wrong with the object library? Could something be screwed up on my
computer? The reason I ask is that I have Office 2000 installed and Outlook
2003 betta version installed as well. Any thoughts?
S. Jackson

S. Jackson
 
D

Douglas J. Steele

I believe you need to use

Dim objOutlook As Outlook.Application

Access also has an Application object in its model. If you don't
disambiguate the reference, it's going to pick the first one it comes to,
which will be Access's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top