VBA : Controlling: Copying data from Excel to Word 
Options: Save as PDF | Save attached file | Toggle line numbers
| Type: |
sub |
| Added By: |
Rembo |
| Short Description: |
Copying data from Excel to Word is best done by creating a Word document object from Excel. That way Excel has a handle to the Word document object which makes it easier to control. |
| Notes: |
You must set a VBE reference to Microsoft Word Object Library first! |
| Added: |
Sep 12 2005 at 11:40 AM |
| Modified: |
Sep 22 2005 at 2:24 PM |
| Related URLs |
http://www.puremis.net/excel/cgi-bin/yabb/YaBB.pl?num=1125666756
|
Run the sub procedure. You do have to select the data to be copied yourself. In
this example I used cells A1 to A3 as an example.
Sub CopyXLSDataToWorddoc()
' You must set a VBE reference to Microsoft Word Object Library first!
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
-
' Create a Word document. To use a document that is already open
' use: Set WordApp = GetObject(, "Word.Application")
Set WordApp = CreateObject("Word.Application")
' Make the newly created Word instance visible
WordApp.Visible = True
-
' Create a new document. To use the active document (if Word is open)
' use: Set WordDoc = WordApp.ActiveDocument
Set WordDoc = WordApp.Documents.Add
-
' Select your data to copy here, for example:
Range("A1:A3").Copy
-
' Paste as unformatted text. For formatted text change
' wdPasteText to wdPasteRTF
WordApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteText, _
Placement:=wdInLine, DisplayAsIcon:=False
-
' Clean up
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub