The IPOutlookApp interface function CreateItem can be used to create new contact, appointment, task, or city items. The function takes a constant indicating which type of item to create (olAppointmentItem, olContactItem, olTaskItem, or olCityItem) and returns an interface pointer of the appropriate type (for example, IAppointment, IContact, ITask, or ICity). When using classes created with the _com_ptr_t template you will need to use the Attach function to take the raw interface pointer returned from CreateItem and attach it to a class object. The following code fragment calls CreateItem to return an IAppointment interface pointer representing a new appointment item. Notice how a raw interface pointer variable is declared (pInterfaceAppt) and passed to the CreateItem function. The pAppt variable is a class object of type IAppointmentPtr created from the _com_ptr_t template class. _COM_SMARTPTR_TYPEDEF(IAppointment, __uuidof(IAppointment)); IAppointment *pInterfaceAppt; IAppointmentPtr pAppt; pOutlookApp->CreateItem(olAppointmentItem, (IDispatch **) &pInterfaceAppt); pAppt.Attach(pInterfaceAppt, FALSE); The Attach function takes the raw interface pointer and attaches it to the pAppt class object, which then takes responsibility for calling Release on the interface pointer. The second argument to Attach specifies whether an AddRef should be automatically called when the attach takes place. Passing FALSE does not result in AddRef being called, and this is appropriate in this case since the AddRef was called by CreateItem. The CreateAppointment function in Listing 14.10 creates a recurring appointment starting on Monday, July3, 2000. The appointment is for 10:00 a.m. and uses the default appointment duration of one hour. The appointment will recur indefinitely every Monday. The code creates smart interface classes for the IAppointment and IRecurrencePattern interfaces. Listing 14.10 Creating a recurring appointment_COM_SMARTPTR_TYPEDEF(IAppointment, __uuidof(IAppointment)); _COM_SMARTPTR_TYPEDEF(IRecurrencePattern, __uuidof(IRecurrencePattern)); void CreateAppointment(IPOutlookAppPtr& pOutlookApp) { IAppointmentPtr pAppt; IAppointment *pInterfaceAppt; IRecurrencePattern *pInterfaceRecur; IRecurrencePatternPtr pRecur; DATE date; SYSTEMTIME st; pOutlookApp->CreateItem(olAppointmentItem, (IDispatch **) &pInterfaceAppt); pAppt.Attach(pInterfaceAppt, FALSE); // Convert Monday, July/3/2000 at 10:00 AM to a date memset(&st, 0, sizeof(SYSTEMTIME)); st.wMonth = 7; st.wDay = 3; st.wYear = 2000; st.wHour = 10; // Convert to date format pOutlookApp->SystemTimeToVariantTime(&st, &date); // Set the subject and start date to 10:00 AM pAppt->put_Subject(_T("Recurring Appointment")); pAppt->put_Start(date); // Set the recurrence pattern pAppt->GetRecurrencePattern(&pInterfaceRecur); pRecur.Attach(pInterfaceRecur, FALSE); pRecur->put_RecurrenceType(olRecursWeekly); pRecur->put_DayOfWeekMask(olMonday); pRecur->put_NoEndDate(VARIANT_TRUE); // Save the appointment pAppt->Save(); cout _T("Appointment added") endl; } The CreateAppointment function initializes a SYSTEMTIME structure with the start time for the appointment. The SystemTimeToVariantTime function converts the date into a variant DATE data type. The variant data type is discussed later in this chapter. The appointment subject is set with the put_Subject function, and the start date with the put_Start function. The IRecurrencePattern interface allows the parameters for a recurring appointment to be set. A IRecurrencePattern interface pointer is obtained by calling the IAppointment interface function GetRecurrencePattern function. The following information is set for the recurrence parameters:
The appointment, together with the recurrence pattern, is saved by calling the IAppointment interface's Save function. The function CreateAppointment is passed a reference to an IPOutlookAppPtr smart pointer class, created using code similar to that contained in Listing 14.9: IPOutlookAppPtr pOutlookApp; HRESULT hr; hr = pOutlookApp.CreateInstance(CLSID_Application); if (FAILED(hr)) { cout _T("Could not create object") endl; return; } hr = pOutlookApp->Logon((long) hWnd); if (FAILED(hr)) cout _T("Could not login") endl; else { CreateAppointment(pOutlookApp); pOutlookApp->Logoff(); }
|