m5u4r3p2h1y
New Member
- Joined
- May 8, 2013
- Messages
- 14
I have the code below which works well. It copies all desired sheets from excel to word and sizes them to fit. However, I would like to insert a page break (or section break) after I have pasted and fitted the selection.
I have tried:
to no avail. I have also tried selecting the bottom of the page then the above code. I have also tried specifying both wdApp and wdDoc. The error I get is "Run-time error '438': Object doesn't support this property or method."
Any thoughts or solutions?
Thanks!
Code:
Sub CopyExcelToWord()
Dim wdApp As Object
Dim wdDoc As Object
Dim WS As Worksheet
'Create a new Word Document
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add
wdApp.Visible = True
'Auto fit and copy excel sheet, paste in to word document and auto fit to window, and repeat
For Each WS In ThisWorkbook.Worksheets
If StrComp(Left(WS.name, 3), "AIP", vbTextCompare) = 0 Then
ActiveSheet.Cells.Select
ActiveSheet.Cells.EntireColumn.AutoFit
ActiveSheet.Cells.EntireRow.AutoFit
ActiveSheet.Cells.Select
Selection.Copy
wdApp.Selection.PasteExcelTable False, False, False
wdDoc.Tables(1).AutoFitBehavior wdAutoFitWindow
End If
Next WS
End Sub
I have tried:
Code:
Selection.InsertBreak Type:=wdSectionBreakNextPage
Any thoughts or solutions?
Thanks!