views:

26

answers:

2

I copied example at http://support.microsoft.com/kb/220595 to the VBA in Excel.

My code follows:

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

Dim olAppt As Outlook.AppointmentItem
Set olAppt = olApp.CreateItem(olAppointmentItem)

I obtained the following error on the line Dim olAppt As Outlook.AppointmentItem: "User-defined type not defined".

How should this be fixed?

I use MS Office 2003.

+1  A: 

There are four prerequisite steps listed in that article. They are listed right before the code block.

You probably forgot to follow the step two, Adding a reference to Outlook object library.
The only difference is, in VBA the menu item is under Tools, not Project.

GSerg
The example uses late binding, which is generally a good idea, so adding a reference to the Outlook library is not necessary nor is it likely to be a good idea if the solution is to be distributed.
Remou
@Remou Not it does not. It uses an unnecessary mix of late binding (`CreateObject`) and early binding (`As Outlook.Application`). Obviously, the early binding bit wins. Yes, you can fix it by redeclaring everything as `Object`, but that does not make sense. You *have* to have Outlook installed to use it's object model, so it doesn't affect the distribution either.
GSerg
@GSerg You are right, I was not paying attention. However, I think it would be best to use late binding because of differences between library versions.
Remou
A: 

You need to refer to all constants by their value, so, as olAppointmentItem = 1 :

 Set olAppt = olApp.CreateItem(1)

You can either look up values, for example http://msdn.microsoft.com/en-us/library/aa911356.aspx, or use Outlook's Object Browser to get the values.

Remou
it seems you misunderstood question
sergdev
The question was "How should this be fixed?" I have frequently seen questions about why "it works on my PC but not on my colleagues" and that is often down to different libraries on each PC. It is easy enough to fix the problem with late binding. So, in my opinion, it should be fixed by using late binding and values for constants.
Remou
thanks! now it is clear
sergdev