Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/170262

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 11:29, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 170262

Article Last Modified on 1/20/2007



APPLIES TO

  • Microsoft Outlook 97 Standard Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition



This article was previously published under Q170262

SUMMARY

Microsoft Outlook 97 provides a complete type library and can be automated to build useful group software and mail-enabled applications. This article illustrates, by example, how to use OLE Automation to create and retrieve properties of Microsoft Outlook 97 mail messages, appointments, and contacts.

MORE INFORMATION

NOTE: To use the following examples, you must create a reference to the "Microsoft Outlook 8.0 Object Library."

Creating and Sending a New Mail Item

The following example illustrates how you can create and send a new mail message with Microsoft Outlook 97. The CreateItem method is used to create a new Outlook item. CreateItem takes only one argument; which specifies the type of item to create. In this example, the argument is a Microsoft Outlook constant, olMailItem, which indicates the creation of a MailItem:

   Sub SendMail()

   Dim objOutlook As New Outlook.Application
   Dim objMail As MailItem

       Set objMail = objOutlook.CreateItem(olMailItem)'Create a new
                                                      'MailItem

       With objMail
           .To = "JohnDoe;JaneDoe"                    'Recipients
           .Subject = "Subject of mail message"       'Subject line of
                                                      'message
           .Body = "Body of mail message"             'Body of mail message
           .Send                                      'Send the message
       End With

       Set objMail = Nothing
       Set objOutlook = Nothing

   End Sub
                

Retrieving the Contents of a Folder

To access one of the default folders (such as the Calendar, Contact or Inbox folders), use the GetDefaultFolder with the NameSpace object. The following example uses the GetDefaultFolder method to list the Subject lines for the contents of the Inbox:

   Sub GetFolderContents ()

   Dim objOutlook As New Outlook.Application
   Dim objNameSpace As NameSpace
   Dim objFolder As MAPIFolder
   Dim i As Integer

       Set objNameSpace = objOutlook.GetNamespace("MAPI")
       Set objFolder = _
         objNameSpace.GetDefaultFolder(olFolderInbox)  'Access the Inbox

       For i = 1 To objFolder.Items.Count              'Loop through all
         Debug.Print objFolder.Items(i).Subject        'items in the Inbox
       Next                                            'and print the
                                                       'Subject line

       Set objFolder = Nothing
       Set objNameSpace = Nothing
       Set objOutlook = Nothing

   End Sub
                

If you wish to access a folder that is not a default folder, traverse the object model to get the folder object. For example, if you wish to access a folder "Personal" under the message store "Mailbox - John Doe," you could use the following:

   Set objFolder = _
      objNameSpace.Folders("Mailbox - John Doe").Folders("Personal")
                

NOTE: Folder names are case-sensitive.

Adding a New Appointment

This next example illustrates how you can add a new appointment to the Microsoft Outlook Calendar folder. Note the similarity between the example creating a new MailItem and this example; both examples use the CreateItem method of the Outlook application object:

   Sub AddAppointment()

   Dim objOutlook As New Outlook.Application
   Dim objAppt As AppointmentItem

       Set objAppt = _
           objOutlook.CreateItem(olAppointmentItem)  'Create a new
                                                     'ContactItem
       With objAppt
           .Subject = "Lunch"               'Appointment's Subject line

                .Start = #2/25/97 1:00:00 PM#    'Appointment's starting
                                                 'time
           .End = #2/25/97 2:00:00 PM#      'Appointment's ending time
           .Location = "Restaurant"         'Appointment's location

           .ReminderSet = True              'Set Reminder to 15 minutes
           .ReminderMinutesBeforeStart = 15 'before the start of the
                                            'appointment
           .Save                            'Save the appointment
       End With

       Set objAppt = Nothing
       Set objOutlook = Nothing

   End Sub
                

Retrieving Appointment Information for a Given Time Frame

You can use the Find method on an Items collection to locate those items that match a specific criteria. The following example shows you how to locate AppointmentItems with a Start date of 2/25/97. The criteria for the Find method is:

   [Start] >= "2/25/97 12:00 AM" and [Start] <= "2/25/97 11:59 PM"
                

With the Find method, the first item matching the criteria is returned. To find subsequent matches, you must use the FindNext method. FindNext will return Nothing when no more matches are found:

   Sub GetAppointments()

   Dim objOutlook As New Outlook.Application
   Dim objNS As NameSpace
   Dim Appt As Object
   Dim objInboxItems As Items
   Dim Criteria as String

       Set objNS = objOutlook.GetNamespace("MAPI")
       Set objInboxItems = _
         objNS.GetDefaultFolder(olFolderCalendar).Items 'Get all items
                                                        'in Calendar folder
       Criteria = "[Start]>=""2/25/97 12:00 AM"" " & _
         "and [Start]<= ""2/25/97 11:59 PM"""           'Criteria for Find
                                                        'method

       Set Appt = objInboxItems.Find(Criteria)     'Find first Appointment
                                                   'matching the criteria

       Do While Not(Appt Is Nothing)               'Loop until no match

         Debug.Print Appt.Start; Appt.End; Appt.Subject 'Print the
                                                        'Appointment Start,
                                                        'End, and Subject

         Set Appt = objInboxItems.FindNext         'Find the next
                                                   'Appointment matching
                                                   'the same criteria
                                                   'used with the Find
                                                   'method
       Loop

       Set Appt = Nothing
       Set objInboxItems = Nothing
       Set objNS = Nothing
       Set objOutlook = Nothing

   End Sub
                

Adding a New Contact

This example illustrates how you can add a new Contact to the Microsoft Outlook Contacts folder. The manner in which you can create new ContactItems is similar to the manner in which you create new AppointmentItems and MailItems, as previously illustrated:

   Sub AddContact()

   Dim objOutlook As New Outlook.Application
   Dim objContact As ContactItem

       Set objContact = objOutlook.CreateItem(olContactItem)

       With objContact

           .FirstName = "John"                   'First Name is "John"

           .LastName = "Doe"                     'Last Name is "Doe"

           .HomeTelephoneNumber = "123-456-7890" 'Home Telephone Number is
                                                 '"123-456-7890"
           .HomeAddressStreet = "123 Main St."   'Street for the Home
                                                 'Address is "123 Main
                                                 'St."
           .HomeAddressCity = "Anycity"          'City for the Home
                                                 'Address is "Anycity"
           .HomeAddressState = "WA"              'State for the Home
                                                 'Address is "WA"
           .HomeAddressPostalCode = "98765"      'Postal Code for the Home
                                                 'Address is "98765"

           .Save                                 'Save the new contact

       End With

       Set objContact = Nothing
       Set objOutlook = Nothing

   End Sub
                

Retrieving Contact Information

A ContactItem can be identified by the Contact's "full name." The FullName property is the default property for a ContactItem:

   Sub GetContactInfo()


   Dim objOutlook As New Outlook.Application
   Dim objContact As ContactItem
   Dim objNameSpace As NameSpace
   Dim objFolder As MAPIFolder


       Set objNameSpace = objOutlook.GetNamespace("MAPI")
       Set objFolder = _
         objNameSpace.GetDefaultFolder(olFolderContacts)'The Contacts
                                                        'Folder


       With objFolder.Items("John Doe")               'Print the
                                                      'HomeAddress
           Debug.Print .HomeAddress                   'and the
           Debug.Print .HomeTelephoneNumber           'HomeTelephoneNumber
       End With                                       'for the Contact with
                                                      'the Name "John Doe"
       Set objFolder = Nothing
       Set objNameSpace = Nothing
       Set objOutlook = Nothing


   End Sub
                

REFERENCES

The Visual Basic for Applications Help file for Microsoft Outlook contains valuable information about the methods, properties, and objects that the Microsoft Outlook 97 object model supports. This Help file, VBAOUTL.HLP, is included in the ValuPack\MoreHelp folder on the Microsoft Office 97 CD-ROM. The Help file is also available for download.

For more information about downloading the Visual Basic for Applications Help file for Microsoft Outlook 97, please see the following article in the Microsoft Knowledge Base:

162671 OL97: Visual Basic Help File for Microsoft Outlook Available



Additional query words: OutSol OutSol97 OutSol98

Keywords: kbautomation kbhowto kbinterop kbprogramming KB170262