Pasting to Word

Orion19

Board Regular
Joined
Dec 18, 2017
Messages
56
Hello!

I'm attempting to copy a range from Excel and paste into a blank word document. The following code works up to the point where the pasting should occur. If I run the macro a new word documents open but it remains blank. The data is copied and makes it to the clipboard but won't paste unless done manually. Any ideas? I'm running Excel for Mac 2017. Here's the code (I need the filter later but am just using it with dummy data now):

Code:
Sub PasteToWord()     
Dim wApp As Word.Application
Dim wDoc As Word.Document


On Error Resume Next
Set wApp = New Word.Application
wApp.Visible = True


Documents.Add DocumentType:=wdNewBlankDocument


wDoc.Content.InsertAfter Range("A1")


     
Range("A1:A4").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$4").AutoFilter Field:=14, Criteria1:=RGB(255 _
        , 0, 0), Operator:=xlFilterCellColor
    Selection.Copy


wDoc.Activate
Selection.Paste


End Sub
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Since you're running the code from Excel, Selection.Paste is assumed to be Excel.Selection.Paste. Regardless, you don't need to activate the document or select anything. All you need is:
wdDoc.Paste
 
Upvote 0
Thanks for the reply!!

I gave that a shot but unfortunately had the same result (only a blank word doc opens). I thought maybe I needed to copy the data before the word doc opens and reorganized the code as follows, but the result is still the same. Any other ideas? Thank you again!!!

Code:
Sub PasteToWord()     
Dim wApp As Word.Application
Dim wDoc As Word.Document


Range("A1:A4").Select
Selection.Copy


On Error Resume Next
Set wApp = New Word.Application
wApp.Visible = True


Documents.Add DocumentType:=wdNewBlankDocument
wDoc.Paste




End Sub
 
Upvote 0
That'll be because you haven't told Word what wDoc is! Instead of:
Documents.Add DocumentType:=wdNewBlankDocument
you should have:
Set wDoc = Documents.Add(DocumentType:=wdNewBlankDocument)
 
Upvote 0
I gave it a shot but still got the same result. The data is in the clipboard but the word document is still blank. I'm really sorry if I'm making obvious missteps. This is my first attempt at communication across applications so I appreciate the help.

I'm going to keep reading but if you have any other ideas I'm all ears (or eye I guess)! Thanks!

That'll be because you haven't told Word what wDoc is! Instead of:
Documents.Add DocumentType:=wdNewBlankDocument
you should have:
Set wDoc = Documents.Add(DocumentType:=wdNewBlankDocument)
 
Upvote 0
The following should work just fine, but I can't see why you'd want to both insert a cell's content and use copy/paste for an overlapping range.
Code:
Sub PasteToWord()
Dim wApp As New Word.Application, wDoc As Word.Document, xlSht As Worksheet
Set xlSht = ActiveSheet
With wApp
  .Visible = True
  Set wDoc = .Documents.Add
  xlSht.Range("$A$1:$A$4").Copy
  With wDoc.Range
    .Paste
    .InsertBefore xlSht.Range("A1")
  End With
End With
Set wDoc = Nothing: Set wApp = Nothing: Set xlSht = Nothing
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top