OL2000: Using Outlook Events in Another Program (225502)



The information in this article applies to:

  • Microsoft Outlook 2000

This article was previously published under Q225502

SUMMARY

This article provides an example of how you can use Outlook events in another program that supports Visual Basic for Applications. Concepts in this article also apply to a custom Visual Basic application.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: IMPORTANT: Before beginning these steps, it is recommended that you make sure that Word is not running and then temporarily rename the Normal.dot file. Word will then create a new Normal.dot template after following these steps. You can then replace the new Normal.dot file with your original one. If you do not have a Normal.dot file for Word, this means that you have not changed any default settings in Word to create the file.

The following example adds code to the Microsoft Word Normal.dot template, so that every time a contact is added in Outlook, you will get a custom printout. This code can also be based in Outlook, but Word is used for illustration purposes. One limitation of having the code in Word is that Word must be running to print the contact.
  1. Start Word 2000.
  2. Press ALT+F11 to open the Visual Basic Editor.
  3. In the Project Explorer window, select the Normal project.
  4. On the Tools menu, click References. Click to select the Microsoft Outlook 9.0 Object Library check box, and then click OK.
  5. On the Insert menu, click Module. Enter the following code into the new Module1 module.
    Private MyEvent As Class1
    Public ol As Outlook.Application
    Public olns As Outlook.NameSpace
    Public ConFolder As Outlook.MAPIFolder
    
    Sub AutoExec()
       Set MyEvent = New Class1
    End Sub
    					
  6. On the Insert menu, click Class Module. Enter the following code into the new Class1 class module.
    Dim WithEvents ConItems As Outlook.Items
    
    Private Sub Class_Initialize()
       Set ol = Outlook.Application
       Set olns = ol.GetNamepace("MAPI")
       Set ConFolder = olns.GetDefaultFolder(olFolderContacts)
       Set ConItems = ConFolder.Items
    End Sub
    
    Private Sub Class_Terminate()
       Set ConItems = Nothing
       Set ConFolder = Nothing
       Set olns = Nothing
       Set ol = Nothing
    End Sub
    
    Private Sub ConItems_ItemAdd(ByVal Item As Object)
    
       ' Add a new document in Word
       Set Doc = Documents.Add
    
       ' Insert sample data into the document.
       With Selection
          .TypeText Item.FullName
          .TypeParagraph
          .TypeText Item.HomeAddress
          .TypeParagraph
          .TypeText "Home: " & Item.HomeTelephoneNumber
          .TypeParagraph
          .TypeText "Work: " & Item.BusinessTelephoneNumber
          .TypeParagraph
       End With
    
       ' Send the document to the default printer
       Doc.PrintOut
    
       ' Close and don't save changes to the document.
       Doc.Close wdDoNotSaveChanges
       Set Doc = Nothing
    End Sub
    					
  7. Close the Visual Basic Editor.
  8. Quit and then restart Word.
  9. Start or switch to Outlook.
  10. Create a contact in Outlook, including a name, home address, and home and business telephone numbers. Save the contact to the default Contacts folder.
When the contact is saved in Outlook, the ItemAdd event will execute in Word. This procedure then generates a printout of the contact information.

REFERENCES

For additional information about available resources and answers to commonly asked questions about Microsoft Outlook 2000 solutions, please see the following article in the Microsoft Knowledge Base:

146636 OL2000: Questions About Custom Forms and Outlook Solutions


Modification Type:MajorLast Reviewed:9/29/2003
Keywords:kbhowto kbProgramming KB225502