web 2.0


Paste Excel range into a Word document using VBA

In this tip you learn how to copy a range from Excel and paste into a Word document. This is a very simple process which does not require much technical knowledge.

I chose to use a generic object rather than a Word object because it does not require the reference of object libraries. The late binding implies a delay, but you can add the reference to the Word library if you wish!

Sub PasteIntoWordDocument()

  Dim oWord As Object
  Dim oWordDoc As Object
  Dim oRng As Excel.Range

  On Error Resume Next
  Set oWord = GetObject(, "Word.Application")
  If Err <> 0 Then Set oWord = CreateObject("Word.Application")

  On Error GoTo
Err_Handler
  Set oRng = Application.InputBox("Select the data range...", , , , , , , 8)

  oRng.Copy
  Set oWordDoc = oWord.Documents.Add
  oWord.Visible = True
  oWordDoc.ActiveWindow.Selection.Paste

  Set oWordDoc = Nothing
  Set oWord = Nothing
  Set oRng = Nothing

  If Err = 0 Then MsgBox "Pasting into Word document was successful!", vbInformation

  Exit Sub
Err_Handler:
  MsgBox Err.Description, vbCritical, Err.Number
  Resume Next
End Sub

Tags: , ,

Microsoft Excel | Microsoft Excel - VBA | Microsoft Word

Comments are closed