XL2000: Macro to Link a Range of Cells in Word (213316)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q213316

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.

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
  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.

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


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbinfo kbinterop kbProgramming kbualink97 KB213316