Microsoft KB Archive/149830

= Macro to Link a Range of Cells in Word =

Article ID: 149830

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 98 for Macintosh
 * Microsoft Word 97 Standard Edition
 * Microsoft Word 95 Standard Edition
 * Microsoft Word 6.0 Standard Edition
 * Microsoft Word 6.0 for Macintosh

-



This article was previously published under Q149830



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 (Sub procedure) in conjunction with OLE Automation technology.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.  In a new Microsoft Excel workbook, type your data in Sheet1. (The following code below assumes that your data is in cells A1:C10.  In Microsoft Excel 97 for Windows and Microsoft Excel 98 Macintosh Edition, press ALT+F11 to start the Visual Basic Editor, click Module on the Insert menu, and then type the macro in this step in the code window of the module.

In Microsoft Excel 5.0 and 7.0, point to Macro on the Insert menu, click Module, and then type the macro in this step in the module sheet. Sub PasteTableToWord

Dim obj As Object Dim temp As String

'Activate the worksheet containing the range to be copied Worksheets("sheet1").Activate

'Select the range the of cells to be copied; update to match 'your data Range("a1:c10").Select

'Copy the cells Selection.Copy

'Create a word object. Set obj = CreateObject("word.basic")

'Create a new file. obj.filenew

'Determine if Microsoft Excel is running on the Macintosh or        'Windows. 'If Microsoft Excel is running on the Macintosh. If (Application.OperatingSystem Like "*Mac*") Then

'Activate Word on the Macintosh AppActivate "Microsoft word"

'Paste the Microsoft Excel Spreadsheet object into Word obj.EditPasteSpecial IconNumber:=0, Link:=1, DisplayIcon:=0, _ Class:="Excel.Sheet.5", DataType:="Object", IconFilename:="", _ Caption:="Microsoft Excel 5.0 Worksheet"

'If Microsoft Excel is running on Windows NT/95/3.x        Else

'If you are using Word 7.0, use this line to make Word visible. ' If Word 6.0 is being used, by default Word starts visible. If (obj.appinfo(2) Like "7.0") Then obj.AppShow

'Paste the Microsoft Excel Spreadsheet object into Word obj.EditPasteSpecial Link:=1, Class:="Excel.Sheet.5", _ DataType:="Object", IconFilename:="", _ Caption:="Microsoft Excel Worksheet"

End If

'Save the file obj.FileSaveAs Name:="testdoc.doc"

'Close Word. Set obj = Nothing

'Return to Microsoft Excel. If this line is not used, the focus 'may be set to another Windows Application AppActivate "Microsoft Excel"

'Deselect the selected range Application.CutCopyMode = False

End Sub  In Excel 97 for Windows and Excel 98 Macintosh Edition, click Close and Return to Microsoft Excel to quit the Visual Basic Editor, activate Sheet1, point to Macro on the Tools menu, and click Macros. Select the PasteTableToWord macro, and then click Run.

In Microsoft Excel 5.0 and 7.0, activate sheet1, click Macro on the Tools menu, select the PasteTableToWord macro, and then click Run.

For additional information about translating Microsoft WordBasic macros and arguments for use with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

120979 How to Use Named WordBasic Arguments in OLE Automation

For additional information about selecting a range in Microsoft Excel using Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

120198 XL: How to Select Cells/Ranges Using Visual Basic Procedures

Additional query words: 8.00 XL

Keywords: kbdtacode kbhowto kbinterop kbprogramming kbualink97 KB149830

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.