XL97: Automation Example with MS Word and MS Outlook

Last reviewed: February 27, 1998
Article ID: Q166812
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

It is possible to create a Visual Basic for Applications macro that uses data from a Microsoft Word document and a Microsoft Excel workbook to send messages from Microsoft Outlook. This article contains an example of one way this may be done.

MORE INFORMATION

The following example assumes that there are two defined names in the worksheet:

  • The first defined name, "subjectcell", refers to a cell that contains the message subject line (for example, "This is a test message.").
  • The second defined name, "tolist", refers to the first cell in the horizontal list that contains a list of recipients (for example, "John Doe", "Jane Doe", etc.).

You must also have a Microsoft Word document. The text of this document is used by the macro as the message body of your mail message.

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

   Sub SendOutlookMessages()

    'Dimension variables.
    Dim OL As Object, MailSendItem As Object
    Dim W As Object
    Dim MsgTxt As String, SendFile As String
    Dim ToRangeCounter As Variant

    'Identifies Word file to send
    SendFile = Application.GetOpenFilename(Title:="Select MS Word 97 " & _
        "file to mail, then click 'Open'", buttontext:="Send", _
        MultiSelect:=False)

    'Starts Word session
    Set W = GetObject(SendFile)

    'Pulls text from file for message body
    MsgTxt = W.Range(Start:=W.Paragraphs(1).Range.Start, _
       End:=W.Paragraphs(W.Paragraphs.Count).Range.End)

    'Ends Word session
    Set W = Nothing

    'Starts Outlook session
    Set OL = CreateObject("Outlook.Application")
    Set MailSendItem = OL.CreateItem(olMailItem)

    ToRangeCounter = 0

    'Identifies number of recipients for To list.
    For Each xCell In ActiveSheet.Range(Range("tolist"), _
        Range("tolist").End(xlToRight))
        ToRangeCounter = ToRangeCounter + 1
    Next xCell

    If ToRangeCounter = 256 Then ToRangeCounter = 1

    'Creates message
    With MailSendItem
        .Subject = ActiveSheet.Range("subjectcell").Text
        .Body = MsgTxt

        'Creates "To" list
        For Each xRecipient In Range("tolist").Resize(1, ToRangeCounter)
            RecipientList = RecipientList & ";" & xRecipient
        Next xRecipient

        .To = RecipientList
        .Send
    End With

    'Ends Outlook session
    Set OL = Nothing

   End Sub


Additional query words: OutSol OutSol97

Keywords : kbinterop kbprg kbhowto
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.