Microsoft KB Archive/213316

= XL2000: Macro to Link a Range of Cells in Word =

Article ID: 213316

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



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:

https://partner.microsoft.com/global/30000104

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

 Start Microsoft Excel and type any data in the range A1:C10 on Sheet1. Press ALT+F11 to start the Visual Basic Editor.  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  On the Tools menu, click References, and then click Microsoft Word 9.0 Object Library. Quit the Visual Basic Editor and return to Excel. On the Tools menu, point to Macro, and then click Macros. Select the PasteTableToWord macro, and then click Run. Notice that macro creates the file C:\TestDoc.doc, which contains data from your Excel spreadsheet.</li></ol>

<div class="references_section">