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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor 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.