Microsoft Knowledge Base Email Alertz

KBAlertz.com: (138283) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 138283 - Last Review: October 11, 2006 - Revision: 1.5

How to Create a Form Letter with a Visual Basic Macro

This article was previously published under Q138283

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:
138289  (http://kbalertz.com/Feedback.aspx?kbNumber=138289/EN-US/ ) 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) Professionals 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

APPLIES TO
  • Microsoft Excel 95 Standard Edition
  • Microsoft Word 95 Standard Edition
Keywords: 
kbprogramming KB138283
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please