XL7: How to Create a Form Letter with a Visual Basic Macro

Last reviewed: September 2, 1997
Article ID: Q138283
The information in this article applies to:
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Word for Windows 95, version 7.0

SUMMARY

When you use OLE Automation to communicate from one program to another, you can complete many useful tasks without having to leave the client program. For example, perhaps you store data in Microsoft Excel, but you would like to create form letters in Microsoft Word using this data. The example provided in the "More Information" section of this article shows how you can perform this task programmatically.

Note that you may experience problems running a macro in Microsoft Excel if you are using a Microsoft Excel data source to perform a mail merge in Microsoft Word. The resolution to this problem is to save the Microsoft Excel file as a Word document and to use the Word document as the source of the data. In the macro example provided below, the macro saves the Microsoft Excel data file to a Word document.

For additional information about this problem, please see the following article in the Microsoft Knowledge Base:

    ARTICLE-ID: Q138289
    TITLE     : XL7: Error Using OLE Automation with Microsoft
                Word 7.0

MORE INFORMATION

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 Product Support Services (PSS) 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.

To use the following macro, you will need to set up the data source in Microsoft Excel. To do so, perform the following steps:

  1. Create a new, blank Microsoft Excel workbook.

  2. Enter the following data on the worksheet:

       A1: NAME        B1: ADDRESS      C1: CITY    D1: STATE    E1: ZIP_CODE
       A2: John Smith  B2: 1 Way Avenue C2: Kent    D2: NC       E2: 11111
       A3: Mary Lynn   B3: Do Drive     C3: Poluk   D3: SC       E3: 22222
       A4: Peter Paul  B4: Fest Drive   C4: Low     D4: FL       E4: 33333
    
    

  3. Save the file as Testdata.xls in the root directory of the C drive, and then close the workbook.

  4. In another workbook, type the following macro code on a module sheet:

          Sub Do_It_All_Form_Letter()
          Dim word As Object
    
          Set word = CreateObject("word.basic") 'creates the word object
    
             With word
                .AppShow ' Makes Word Visible
    
                'Sends keystrokes to the Open Worksheet dialog box
                SendKeys "{TAB}{TAB}{TAB}{ENTER}"
    
                'Opens the Microsoft Excel file
                .FileOpen Name:="c:\testdata.xls"
    
                'Saves the file as a Microsoft Word document
                .FileSaveAs Name:="c:\testdata.doc", Format:=0
    
                .FileClose 'Closes the file
                .FileNewDefault ' Opens up blank Word document
    
                'Makes the active window a main document
                .MailMergeMainDocumentType 0
    
                'Open the data source
                .MailMergeOpenDataSource Name:="c:\testdata.doc", _
                LinkToSource:=0
    
                'Activates the mail merge main document
                .MailMergeEditMainDocument
    
       'The following Insert commands place text into the Word
       'document. You could change these commands to place any text in
       'the document. This example uses a typical business letter
       'format.
    
                .Insert "3454 Blindside St."    'Inserts a string
                .InsertPara                     'Inserts a carriage return
                .Insert "Columbia, GA 23287"
                .InsertPara
                .InsertPara
    
          'the mergefields are the same as the column headings in the Excel
          'workbook
          .InsertMergeField MergeField:="NAME"
               .InsertPara
               .InsertMergeField MergeField:="ADDRESS"
               .InsertPara
               .InsertMergeField MergeField:="CITY"
               .Insert ", "
               .InsertMergeField MergeField:="STATE"
               .Insert " "
               .InsertMergeField MergeField:="ZIP_CODE"
               .InsertPara
               .InsertPara
               .InsertPara
               .Insert "Dear "
               .InsertMergeField MergeField:="NAME"
               .Insert ","
               .InsertPara
               .InsertPara
               .Insert "Thank You For Your Support."
               .InsertPara
               .InsertPara
               .InsertPara
               .Insert "Sincerely,"
               .InsertPara
               .InsertPara
               .InsertPara
               .Insert "John M. Doe"
               .MailMergeToDoc 'Merges data records with the main document
               ' Saves the active document with the specified name
          .FileSaveAs Name:="c:\letters.doc"
    
          End With
    
       Set word = Nothing 'clears the object variable
    
       End Sub
    
    

  5. Run the macro.

The sample file Letters.doc is now available on the root directory of the computer's C drive.

REFERENCES

For more information about OLE Automation, click the Index tab in Microsoft Excel Help, and type the following text:

   OLE


Additional query words: 7.00 mail merge
Keywords : kbprg PgmHowTo xlwin kbinterop kbole kbprg
Version : 7.00
Platform : WINDOWS


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: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.