Microsoft KB Archive/291119

= OL2002: Using Outlook Events in Another Program =

Article ID: 291119

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q291119





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:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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.  Start Word 2002. Press ALT+F11 to open the Visual Basic Editor. In the Project Explorer window, select the Normal project. On the Tools menu, click References. Click to select the Microsoft Outlook 10.0 Object Library check box, and then click OK.  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   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.GetNamespace(&quot;MAPI&quot;) 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 &quot;Home: &quot; & Item.HomeTelephoneNumber .TypeParagraph .TypeText &quot;Work: &quot; & 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 </li> Close the Visual Basic Editor.</li> Quit and then restart Word.</li> Start or switch to Outlook.</li> Create a contact in Outlook, including a name, home address, and home and business telephone numbers. Save the contact to the default Contacts folder.</li></ol>

When the contact is saved in Outlook, the ItemAdd event will execute in Word. This procedure then generates a printout of the contact information.

<div class="references_section">