XL: Macro to Link a Range of Cells in Word

Last reviewed: February 3, 1998
Article ID: Q149830
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for the Macintosh, version 5.0
  • Microsoft Excel 98 Macintosh Edition
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows
  • Microsoft Word for Windows, version 6.0
  • Microsoft Word for the Macintosh, version 6.0
  • Microsoft Word for Windows 95, version 7.0
  • Microsoft Word 97 for Windows

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, 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 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:

       ARTICLE-ID: Q120979
       TITLE     : 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:

       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
    Keywords : kbcode kbinterop kbprg kbualink97 PgmHowto
    Version : WINDOWS: 5.0, 5.0c, 7.0, 97; MACINTOSH: 5.0, 5.0a, 98
    Platform : MACINTOSH WINDOWS
    Issue type : kbhowto


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: February 3, 1998
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.