XL: Macro to Link a Range of Cells in WordLast reviewed: February 3, 1998Article ID: Q149830 |
The information in this article applies to:
SUMMARYThis 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 INFORMATIONMicrosoft 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 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.asp 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 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:
ARTICLE-ID: Q120979 TITLE : How to Use Named WordBasic Arguments in OLE AutomationFor additional information about selecting a range in Microsoft Excel using Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
ARTICLE ID: Q120198 TITLE : XL: How to Select Cells/Ranges Using Visual Basic Procedures |
Additional query words: 5.00 6.00 7.00 8.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |