PDA

View Full Version : Pasting from Excel to word



Lartk
01-10-2013, 01:12 PM
Is there a code that allows you to paste something from excel to a word document? But i am working in excel. I tried something like this but it doesn't work.

[VBA]Sub OpenWord()
Workbooks.Open Filename:= _
"C:\Users\klartigue\Desktop\Master.doc"
End Sub[\VBA]

Lartk
01-10-2013, 01:15 PM
Sub OpenWord()
Workbooks.Open Filename:= _
"C:\Users\klartigue\Desktop\Master.doc"
End Sub

Lartk
01-10-2013, 01:25 PM
In a Master.xls sheet, i am trying to paste range A5:H50 in sheet 1 into a word document.

Lartk
01-10-2013, 01:31 PM
Maybe something like this? But it says user - defined type not defined for the DIM part.

Sub PasteToWord()

Dim AppWord As Word.Application

Set AppWord = CreateObject("Word.Application")
AppWord.Visible = True
' Change the range to suit your needs. See the How to Use for this code
Sheets("Sheet1").Range("A4:H30").Copy
AppWord.Documents.Add
AppWord.Selection.Paste

Application.CutCopyMode = False

Set AppWord = Nothing

End Sub

Kenneth Hobs
01-10-2013, 01:39 PM
http://vbaexpress.com/forum/showthread.php?p=178364

Lartk
01-10-2013, 01:50 PM
Nevermind, I figured it out! I had to go to tools and reference word.

Sub PasteToWord()

Dim AppWord As Word.Application

Set AppWord = CreateObject("Word.Application")
AppWord.Visible = True
' Change the range to suit your needs. See the How to Use for this code
Sheets("Sheet1").Range("A3:H30").Copy
AppWord.Documents.Add
AppWord.Selection.Paste

Application.CutCopyMode = False

Set AppWord = Nothing

End Sub

Lartk
01-10-2013, 02:15 PM
Kenneth - the link you gave me above is actually very helpful. I like this code:

Option Explicit
Sub CopyWorksheetsToWord()
' requires a reference to the Word Object library:
' in the VBE select Tools, References and check the Microsoft Word X.X object library
Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet
Application.ScreenUpdating = False
Application.StatusBar = "Creating new document..."
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Add
For Each ws In ActiveWorkbook.Worksheets
Application.StatusBar = "Copying data from " & ws.Name & "..."
ws.UsedRange.Copy ' or edit to the range you want to copy
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
Application.CutCopyMode = False
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
' insert page break after all worksheets except the last one
If Not ws.Name = Worksheets(Worksheets.Count).Name Then
With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
.InsertParagraphBefore
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
End If
Next ws
Set ws = Nothing
Application.StatusBar = "Cleaning up..."
' apply normal view
With wdApp.ActiveWindow
If .View.SplitSpecial = wdPaneNone Then
.ActivePane.View.Type = wdNormalView
Else
.View.Type = wdNormalView
End If
End With
Set wdDoc = Nothing
wdApp.Visible = True
Set wdApp = Nothing
Application.StatusBar = False
End Sub


Is there a way to alter it to only copy range A5:H30 in excel (for each sheet) and paste them each in a new page in word. So each sheet in excel would get its own sheet in word?

Kenneth Hobs
01-10-2013, 02:21 PM
For the copy line replace with:
ws.Range("A3:H30").Copy

Lartk
01-10-2013, 02:26 PM
I figured out how to get the range I want:

ws.Range("A5:H30").Copy ' or edit to the range you want to copy

but how can I get each sheet from excel to each paste into a new page in word?

Lartk
01-11-2013, 10:16 AM
Any way to have each sheet in excel paste into a new sheet in word?

Kenneth Hobs
01-11-2013, 10:28 AM
I does that. Look at page view in MSWord.

It should go without saying that a range may not fit on a single MSWord page.