I have a word document that I create from data in an excel spreadsheet to create reports. I need to insert a page break at the end of each report. The code I have is below. The error I receive is:
Object doesn't support this property or method.
Dim PrintFilter As String
Dim wshT As Worksheet
Dim wbkT As Workbook
Dim wshS As Worksheet
Dim wbkS As Workbook
Dim wdApp As Word.Application
Dim myDoc As Word.Document
Dim mywdRange As Word.Range
Dim ReportName As Word.Range
Dim ProjectLeader As Word.Range
Dim ProjectStatus As Word.Range
Dim StatusDate As Word.Range
Dim BackgroundInfo As Word.Range
Dim StatusInfo As Word.Range
Dim NextStepsInfo As Word.Range
Dim newDoc As Word.Document
Dim docrng As Word.Range
Const wdPageBreak As Long = 7
On Error GoTo ErrorHandler
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Application.ScreenUpdating = False
Set wbkT = ActiveWorkbook
Set wshT = ActiveSheet
PrintFilter = wshT.Range(Target.Parent.Address).Offset(0, -1).Value
Set wbkS = Workbooks.Open(Filename:="ProjectList.xlsm", ReadOnly:=True)
If PrintFilter = "0" Then
Set wdApp = New Word.Application
Set newDoc = wdApp.Documents.Add
For i = 1 To 2
PrintFilter = wshT.Range(Target.Parent.Address).Offset(i, -1).Value
If PrintFilter <> "" Then
If i > 1 Then
With newDoc
.InsertBreak Type:=wdPageBreak 'Error on this line
End With
End If
Set wshS = wbkS.Worksheets(PrintFilter)
wshS.Range("B10,B12,B15").NumberFormat = "@"
wshS.Range("B10,B12,B15").Font.Name = "Georgia"
wshS.Range("B10,B12,B15").Font.Size = 11
wshS.Range("B10,B12,B15").WrapText = True
wshS.Range("B10,B12,B15").Rows.AutoFit
' Instatiate the Word Objects.
Set wdDoc = wdApp.Documents.Open(Filename:="Project Status Reports Template.docx", ReadOnly:=True)
With wdDoc
Set ReportName = .Bookmarks("ReportName").Range
Set ProjectLeader = .Bookmarks("ProjectLeader").Range
Set ProjectStatus = .Bookmarks("ProjectStatus").Range
Set StatusDate = .Bookmarks("StatusDate").Range
Set BackgroundInfo = .Bookmarks("BackgroundInfo").Range
Set StatusInfo = .Bookmarks("StatusInfo").Range
Set NextStepsInfo = .Bookmarks("NextStepsInfo").Range
End With
' Write values to the bookmarks.
ReportName.Text = wshS.Cells(4, 2).Text
ProjectLeader.Text = wshS.Cells(7, 2).Text
ProjectStatus.Text = wshS.Cells(8, 2).Text
StatusDate.Text = wshS.Cells(15, 1).Text
BackgroundInfo.Text = wshS.Cells(10, 2).Text
StatusInfo.Text = wshS.Cells(15, 2).Text
NextStepsInfo.Text = wshS.Cells(12, 2).Text
wdDoc.Content.Copy
Set docrng = newDoc.Content
docrng.Collapse Direction:=wdCollapseEnd
docrng.Paste
With wdDoc
.Close SaveChanges:=wdDoNotSaveChanges
End With
End If
Next i
With newDoc
.ExportAsFixedFormat OutputFileName:=(Format(Now(), "yyyymmddHhNnSs") & PrintFilter & "PDFFile") & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=True, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True
.Close SaveChanges:=wdDoNotSaveChanges
End With
wdApp.Quit
Bookmarks