Scriptorium

us fr nl




VBA : Controlling: Copying data from Excel to Word   us


Options: Save as PDF | Save attached file | Toggle line numbers

Details:

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


Usage:

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.


Code:

Formatted | Unformatted
  1. Sub CopyXLSDataToWorddoc()
  2. ' You must set a VBE reference to Microsoft Word Object Library first!
  3.  
  4. Dim WordApp As Word.Application
  5. Dim WordDoc As Word.Document
  6. ' Create a Word document. To use a document that is already open
  7. ' use: Set WordApp = GetObject(, "Word.Application")
  8. Set WordApp = CreateObject("Word.Application")
  9.  
  10. ' Make the newly created Word instance visible
  11. WordApp.Visible = True
  12. ' Create a new document. To use the active document (if Word is open)
  13. ' use: Set WordDoc = WordApp.ActiveDocument
  14. Set WordDoc = WordApp.Documents.Add
  15. ' Select your data to copy here, for example:
  16. Range("A1:A3").Copy
  17. ' Paste as unformatted text. For formatted text change
  18. ' wdPasteText to wdPasteRTF
  19. WordApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteText, _
  20. Placement:=wdInLine, DisplayAsIcon:=False
  21. ' Clean up
  22. Set WordDoc = Nothing
  23. Set WordApp = Nothing
  24. End Sub



User comments :

Add a new comment   Back to Top
Atom Feed
Contact | About This Application | Scriptorium Website