copying sheets into Word

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have excel file which has multiple sheets. each sheet has text and pictures. The text is simple one each line written in one cell and each sheet has several lines of text. I want to copy all these sheets, 20 of them into Microsoft word. What I did, I grouped all sheets and then Ctrl+A the first sheet and then paste it in Word. But only sheet1 got copied. Any idea how I can copy them all at one shot? Thank you very much.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi lezawang. You can give this a trial. Change the Word doc file path to suit. HTH. Dave
Module code....
Code:
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function CloseClipboard Lib "user32" () As Long
    Public Declare Function EmptyClipboard Lib "user32" () As Long
Sheet code...
Code:
Sub SaveXlRangeToWordFile()
Dim Ws As Worksheet, WidthAvail As Double
Dim WdDoc As Object, WdApp As Object, Cnt As Integer
'open Word application
On Error Resume Next
Set WdApp = GetObject(, "word.application")
If Err.Number <> 0 Then
On Error GoTo 0
Set WdApp = CreateObject("Word.Application")
End If
'open doc **********change file path to suit
On Error GoTo erfix
Set WdDoc = WdApp.Documents.Open(Filename:="C:\yourfoldername\bart.docx")
'clear doc
With WdApp.ActiveDocument
    .Range(0, .Characters.Count).Delete
End With
'determine width
With WdApp.ActiveDocument.PageSetup
    WidthAvail = .PageWidth - .LeftMargin - .RightMargin
End With
'loop sheets
For Each Ws In ActiveWorkbook.Worksheets
Cnt = Cnt + 1
Application.StatusBar = "Copying data from " & Ws.Name & "..."
Ws.UsedRange.Copy
WdDoc.Paragraphs(WdDoc.Paragraphs.Count).Range.PasteSpecial DataType:=3 '9  '4
'size range to sheet
With WdDoc.Shapes(Cnt)
.LockAspectRatio = msoFalse
.Width = WidthAvail
End With
Application.CutCopyMode = False
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
'paste to seperate page
WdDoc.Paragraphs(WdDoc.Paragraphs.Count).Range.InsertParagraphAfter
If Not Ws.Name = Worksheets(Worksheets.Count).Name Then
With WdDoc.Paragraphs(WdDoc.Paragraphs.Count).Range
.InsertParagraphAfter
.Collapse Direction:=0 'wdCollapseEnd
.InsertBreak Type:=7 'wdPageBreak
End With
End If
Next Ws
'clean up
WdApp.ActiveDocument.Close savechanges:=True
Set WdDoc = Nothing
WdApp.Quit
Set WdApp = Nothing
Application.StatusBar = False
Exit Sub

erfix:
On Error GoTo 0
MsgBox "Save SaveXlRangeToWordFile error"
WdApp.ActiveDocument.Close savechanges:=False
Set WdDoc = Nothing
WdApp.Quit
Set WdApp = Nothing
Application.CutCopyMode = False
Application.StatusBar = False
End Sub
 
Upvote 0
Solution
Much simpler - and in all likelihood faster:
VBA Code:
Sub SendExcelSheetsToWordDocument()
'Note: This code requires a VBA reference to be set to Word, via Tools|References.
Dim StrNm As String
StrNm = "C:\Users\" & Environ("UserName") & "\Documents\Filename.docx"
If Dir(StrNm, vbNormal) = "" Then
  MsgBox "Cannot Find: " & StrNm: Exit Sub
End If
Dim xlWs As Worksheet, WdApp As New Word.Application, WdDoc As Word.Document
WdApp.Visible = False
Set WdDoc = WdApp.Documents.Open(Filename:=StrNm, AddToRecentFiles:=False)
For Each xlWs In ThisWorkbook.Worksheets
  Application.StatusBar = "Processing: " & xlWs.Name
  xlWs.UsedRange.Copy
  With WdDoc
    .Range.InsertAfter Chr(12)
    .Characters.Last.Paste
  End With
Next
Application.StatusBar = False
WdDoc.Close SaveChanges:=True: WdApp.Quit
Set WdDoc = Nothing: Set WdApp = Nothing: Set xlWs = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,548
Messages
6,120,141
Members
448,948
Latest member
spamiki

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