Insert page break in Word document using macro in Excel

I am trying to have my macro make three tables in Word. It currently makes one big table and formats it correctly, but I can’t find how to force a page break at the desired times. I found a macro that was using vba in Word and they used .InsertBreak but this keeps causing an error when I run it.

Here is my code at present. It’s pieced together from a lot of sources with bits of my own chucked in there;

Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim wdTbl As Word.Table
Dim xlSht As Worksheet
Dim lRow As Integer
Dim lCol As Integer
Dim r As Integer
Dim c As Integer
Dim Blanks As Integer
Dim First As Integer
Dim Second As Integer

lRow = Sheets("Feedback Sheets").Range("A1000").End(xlUp).Row - 2


Blanks = 0
i = 1
    Do While i <= lRow
    Set rRng = Worksheets("Feedback Sheets").Range("A" & i)

        If IsEmpty(rRng.Value) Then
        
            Blanks = Blanks + 1
            
                If Blanks = 1 Then First = i
                If Blanks = 2 Then Second = i
                
        End If
        
        i = i + 1
    Loop
'Now I know where end of each table is

Set xlSht = ActiveSheet: lCol = 5
With wdApp
  .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc
    Set wdTbl = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
    With wdTbl
      .Rows(1).Range.Font.Bold = True
      .Rows(1).HeadingFormat = True
      .Cell(1, 1).Range.Text = "Header 1"
      If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
      If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
    End With
    With xlSht
      For r = 1 To lRow
        If (r + 1) > wdTbl.Rows.Count Then wdTbl.Rows.Add
        If r = First Then wdDoc.InsertBreak

        If r = Second Then wdDoc.InsertBreak

        For c = 1 To lCol
          wdTbl.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
        Next c
      Next r
    End With
  End With
End With

Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
 .InsideLineStyle = wdLineStyleSingle
 .OutsideLineStyle = wdLineStyleDouble
End With

Set wdTbl = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing: Set xlSht = Nothing

When I run it I get runtime error ‘438’, object doesn’t support this property or method and the Debug highlights this line;

If r = First Then wdDoc.InsertBreak

which is why I think it’s the InsertBreak that is the issue.

Edit: I have now managed to get it to make three tables with page breaks after each table, but over three different documents (and probably in the most clunky way possible). When I remove the Set wdDoc = .Documents.Add line from the second and third iterations of that block of code it just creates table 3. I feel like it might have the table selected after applying the formatting and then overwriting it with the next table, but setting Set myTable = Nothing ruins everything.

Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim wdTbl1 As Word.Table
Dim wdTbl2 As Word.Table
Dim wdTbl3 As Word.Table
Dim xlSht As Worksheet
Dim lRow As Integer
Dim lCol As Integer
Dim r As Integer
Dim c As Integer
Dim Blanks As Integer
Dim First As Integer
Dim Second As Integer

lRow = Sheets("Feedback Sheets").Range("A1000").End(xlUp).Row - 2


Blanks = 0
i = 1
    Do While i <= lRow
    Set rRng = Worksheets("Feedback Sheets").Range("A" & i)

        If IsEmpty(rRng.Value) Then
        
            Blanks = Blanks + 1
            
                If Blanks = 1 Then First = i
                If Blanks = 2 Then Second = i
                
        End If
        
        i = i + 1
    Loop
'Now I know where end of each table is

Set xlSht = ActiveSheet: lCol = 5
With wdApp
  .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc
    Set wdTbl1 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
    With wdTbl1
      .Rows(1).Range.Font.Bold = True
      .Rows(1).HeadingFormat = True
      .Cell(1, 1).Range.Text = "Header 1"
      If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
      If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
    End With
    With xlSht
      For r = 1 To First
        If (r + 1) > wdTbl1.Rows.Count Then wdTbl1.Rows.Add
        If r = First Then wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
        For c = 1 To lCol
          wdTbl1.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
        Next c

      Next r
    End With
  End With
End With

Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
 .InsideLineStyle = wdLineStyleSingle
 .OutsideLineStyle = wdLineStyleDouble
End With


  
With wdApp
 .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc
    Set wdTbl2 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
    With wdTbl2
      .Rows(1).Range.Font.Bold = True
      .Rows(1).HeadingFormat = True
      .Cell(1, 1).Range.Text = "Header 1"
      If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
      If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
    End With
    With xlSht
      For r = First To Second
        If (r + 1) > wdTbl2.Rows.Count Then wdTbl2.Rows.Add

        If r = Second Then wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak

        For c = 1 To lCol
          wdTbl2.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
        Next c
      Next r
    End With
  End With
End With

Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
 .InsideLineStyle = wdLineStyleSingle
 .OutsideLineStyle = wdLineStyleDouble
End With

With wdApp
 .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc
    Set wdTbl3 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
    With wdTbl3
      .Rows(1).Range.Font.Bold = True
      .Rows(1).HeadingFormat = True
      .Cell(1, 1).Range.Text = "Header 1"
      If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
      If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
    End With
    With xlSht
      For r = Second To lRow
        If (r + 1) > wdTbl3.Rows.Count Then wdTbl3.Rows.Add

        If r = Second Then wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak

        For c = 1 To lCol
          wdTbl3.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
        Next c
      Next r
    End With
  End With
End With

Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
 .InsideLineStyle = wdLineStyleSingle
 .OutsideLineStyle = wdLineStyleDouble
End With


Set wdTbl1 = Nothing: Set wdTbl2 = Nothing: Set wdTbl3 = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing: Set xlSht = Nothing

Edit 2: This seems to have strayed too far from the original question so I’ll mark this one as done and ask a new one.

7

wdDoc.InsertBreak is invalid, as you would know if you’re using IntelliSense, because the document object does not have an InsertBreak method.

InsertBreak is a method of the Range object. As you want to place the break after the table you should use:

wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật