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.
 
S

S Jackson

Dan:

I think the little light is starting to flicker here, but it still ain't
working for me. Here is the error I am getting:

Error438
Object doesn't support this property or method.

Here is my code corrected as you suggested:

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 Object
Dim objTask As Object

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

What is wrong here? What am I still not getting?

S. Jackson
 
D

Dan Artuso

Hi,
Ooops! forgot to mention one important thing (which was mentioned by
someone else in the original thread).

When you don't have a reference set, you can no longer use the constants
that were defined in that library. So you can't use olTaskItem, you have
to use the numerical value.

So how do you find that value?
With a reference set to Outlook, open up the Object Browser.
From any code module press F2.

The Object Browser is a great thing. It allows you to see all the properties and methods
of classes within a library.

Select Outlook from the combo that is now displaying "All Librairies"

By default, <globals> will be selected in the classes list box.
The right hand pane will show you all the members.

Scroll down until you get to olTaskItem.
Click on it and in the bottom pane you'll see the value, which should be 3.

Now you can use that value directly in place of olTaskItem, or you can define your own constants
to make it more readable.
For example you could put this in the declarations section of a standard module:
Public Const oTaskItem= 3

Also, it may be wise to disable your error handling so that the code will break on the
offending line, that way you always know what's causing the error.
 
S

S Jackson

Okay, now the light has come on. It makes sense now. Thanks for your help.
I will give it a try. I misunderstood when you posted earlier that "[t]he
TaskItem object is created by your Outlook Application variable." For some
reason, I figured that somehow it magically knew what to do with it (duh!).

S. Jackson
 
S

S Jackson

I took a look at this and I think I understand what you are saying, but if I
haven't already said, I'm brand new to VBA and struggling along with a book,
this newsgroup and trial and error. I am not getting the global concept I
think is what my problem is.

I created Module 1 and put in this text under Declarations:

Public Const olTaskItem=3

But, aren't we missing something here?

In my private procedure, I declare the variables, then set variable
objOutlook to CreateObject (Outlook.Application)
Then I want to use a Function within Outlook.Application called CreateItem
when I set the variable objTask (objTask=objOutook.CreateItem(olTaskItem)

How do I declare this as a public function - or is that what I need to do?

S. Jackson
TIA
 
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