Using Outlook Objects

As you have already learned, Outlook is the application in Office that manages e-mail, appointments, contact information, and other tasks related to scheduling and communicating in the workplace. Outlook doesn't contain its own Visual Basic Editor for building macros, but it does have an object library that you can use to run its commands from other Office macros.

Creating the SendMail Macro

In this section, you'll see how to send e-mail via Outlook from within a Word macro named SendMail. Before you create the SendMail macro, you'll need to edit the code in the Command1_Click event procedure to use e-mail names that fit your particular situation. Don't use the ones shown, because they're not real— you'll get an error back from your e-mail service provider if you try them. If you want to send a sample attachment, you'll also need to place the Guitar.bmp file in your root folder (C:\) or substitute a different pathname in the code.

The SendMail macro is located in the Chap62 document on the Running Office Reader's Corner page. The Guitar.bmp file (required as an attachment) is also on the Reader's Corner page.

Follow these steps to create the macro:

  1. In Word, choose Macros from the Macro submenu of the Tools menu.
  2. Type SendMail, and then click Create.
  3. Choose References from the Visual Basic Tools menu.
  4. Verify that the Microsoft Office 9.0 Object Library is selected.
  5. Click OK to add the reference to your project.

    Visual Basic adds the Office object library to your project.

  6. Type the following code in the SendMail macro using the Code window.
  7.  Set out = CreateObject("Outlook.Application")    With out.CreateItem(olMailItem) 'using the Outlook object      'insert recipients one at a time with the Add method      '(these names are fictitious -- replace with your own)      .Recipients.Add "" ''To: field      .Recipients.Add """ 'To: field      .Subject = "Test Message"  'include a subject field      .Body = Selection.Text  'copy selected text for message      'insert attachments one at a time with the Add method      .Attachments.Add "c:\guitar.bmp"      'finally, copy message to Outlook outbox with Send      .Send  End With

  8. Change the dummy e-mail names ( and to real names. This step is necessary to fully test how the macro sends mail over the Internet.
  9. Click the Save button to save the macro to disk.

Running the SendMail Macro

Follow these steps to run the macro and send electronic mail via Outlook:

  1. Use the Windows Start menu to start Outlook if it isn't already running.

    You'll want to verify that Visual Basic puts the e-mail message in Outlook's Outbox folder.

  2. Click the View Microsoft Word button to return to Word's Normal view.
  3. Select a paragraph of sample text in the Word document. (This text will become the body of your mail message.)
  4. Press Alt+F8 to open the Macros dialog box, and then double-click the SendMail macro.

    Visual Basic uses Automation to create a mail message in the Outlook Outbox folder. The message will remain in Outlook's Outbox folder until you connect to your e-mail service and choose Check For New Mail from the Outlook Tools menu.

  5. Now restore Outlook, and click the Outbox folder to see your message.

    You'll see a pending mail message, as shown in the following illustration:

    click to view at full size.

  6. Double-click the message in the Outbox.

    Outlook opens the message and displays it on the screen. (See Figure 41-2.) As you can see, Word sent the message exactly as you directed, complete with a guitar attachment, if you downloaded it from the Web to your root folder.

  7. Click the Send button to mark the message for mailing (again) in the Outbox. Now you can either send or delete the message.
  8. If you want to delete the test message, select the message, and press Delete. To send the message, choose Check For New Mail from the Tools menu.

You're finished working with Outlook in this chapter.

click to view at full size.

Figure 41-2. This Outlook mail message was created by Word's SendMail macro.

Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228 © 2008-2017.
If you may any questions please contact us: