VBA insert page break in Word

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.

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
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!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Found the solution:
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


        With wdApp.Selection
        .InsertBreak Type:=7
        End With


    End If
    
   Next WS
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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