Writing an Outlook Macro

Outlook 2002 supports macro code that automates repetitive tasks. Unlike some other members of the Office XP family, Outlook does not feature a macro recorder that will write VBA code in response to user commands and actions. From the standpoint of Outlook VBA, an Outlook macro is a public Sub procedure without arguments in a standard module. If you add arguments to your module-level Sub procedure, it will no longer be available as a macro. An Outlook macro cannot utilize a Function procedure or private Sub procedure. Also, you cannot place macro code in a class module such as ThisOutlookSession or a form module. Remember that ThisOutlookSession represents a class module rather than a standard module. If you want to write a Sub procedure that you can use as a macro, you must first insert a standard module into your Outlook project and then insert a Sub procedure into that module. Follow these steps to create a macro that launches Microsoft Word 2002 and creates a Word mail message:

To create a new Outlook Macro

1. In Outlook, point to Macro on the Tools menu and then click Visual Basic Editor.

2. On the Tools menu, click References. Use the References dialog box to set a reference to the Microsoft Word 10.0 Object Library. Click OK.

3. Select Projectl in the Project Explorer. If you want to insert a new module, click Module on the Insert menu of the VBA Editor window. Otherwise, double-click the name of the module where you want to insert the macro in the Project Explorer window.

4. On the Insert menu, click Procedure.

5. In the Name box, type LaunchWordMail for the macro name, as shown in Figure 9-5. The name cannot contain spaces.

7. The LaunchWordMail Sub procedure appears in the Code window.

8. Type the following code in the body of the Sub procedure:

Sub LaunchWordMail() On Error Resume Next Dim wdApp As Word.Application Dim wdDoc As Word.Document Set wdApp = CreateObject("Word.Application")

Set wdDoc = wdApp.Documents.Add(DocumentType:=wdNewEmailMessage) wdApp.Visible = True wdApp. ActiveWindow.EnvelopeVisible = True End Sub

0 0

Post a comment