Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Macro to Link a Range of Cells in Word


View products that this article applies to.

Summary

This article provides an example of how to link a Microsoft Excel worksheet to a Microsoft Word document using a Microsoft Visual Basic for Applications macro in conjunction with OLE Automation technology.

↑ Back to the top


More information

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
  1. Start Microsoft Excel and type any data in the range A1:C10 on Sheet1.
  2. Press ALT+F11 to start the Visual Basic Editor.
  3. On the Insert menu, click Module, and then type or paste the following code:
    Sub PasteTableToWord()
        Dim obj As Word.Application
    
        Worksheets("sheet1").Activate  'Activate the worksheet
        'Select the range of cells to copy       
        Worksheets("sheet1").Range("a1:c10").Copy
          
        Set obj = CreateObject("Word.Application.9")  'Create a word object
        obj.Visible = True 'Make Word visible
        Set newDoc = obj.Documents.Add  'Create a new file.
       
        'Determine if Microsoft Excel is running on the Macintosh or Windows.
        If (Application.OperatingSystem Like "*Mac*") Then
            AppActivate "Microsoft word"
            obj.Selection.PasteSpecial  'Paste data into Word
          
        Else  'If Windows NT/95/3.x - paste data into Word
            obj.Selection.PasteSpecial
        End If
       'Format table
        obj.Selection.Tables(1).AutoFormat Format:=wdTableFormatGrid1 
        newDoc.SaveAs Filename:="C:\TestDoc.doc"  'Save the file
        obj.Quit  'Quit Word
        Set obj = Nothing 'Release object
    End Sub
    					
  4. On the Tools menu, click References, and then click Microsoft Word 9.0 Object Library.
  5. Quit the Visual Basic Editor and return to Excel.
  6. On the Tools menu, point to Macro, and then click Macros.
  7. Select the PasteTableToWord macro, and then click Run. Notice that macro creates the file C:\TestDoc.doc, which contains data from your Excel spreadsheet.

↑ Back to the top


References

For additional information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:
226118 OFF2000: Programming Resources for Visual Basic for Applications
For additional information about running Microsoft Word macros from Microsoft Excel, click the article number below to view the article in the Microsoft Knowledge Base:
213430 XL2000: How to Run a WordBasic Macro from a Microsoft Excel Macro

↑ Back to the top


Keywords: KB213316, kbualink97, kbprogramming, kbinterop, kbinfo, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213316
Revision : 10
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 293