The following should work
Option Explicit 'Graham Mayor - https://www.gmayor.com - Last updated - 14 Dec 2020 Private oApp As Object 'application Private oDoc As Object 'document Private oRng As Object, orngBreak As Object 'Range objects Sub Test() Dim lCount As Long Dim lLineNumber As Long Dim lPageCount As Long Dim sText As String Dim lWdPageNumber As Long On Error Resume Next Set oApp = GetObject(, "Word.Application") If Err Then Set oApp = CreateObject("Word.Application") End If On Error GoTo 0 Set oDoc = oApp.Documents.Add Set oRng = oDoc.Range For lCount = 1 To 60 'get current line number and page number before inserting text lLineNumber = oRng.Information(10) 'wdFirstCharacterLineNumber lPageCount = oRng.Information(3) 'wdActiveEndPageNumber oRng.Collapse 0 oRng.Text = "Text to test" & lCount & vbCr lWdPageNumber = oRng.Information(3) If lWdPageNumber > lPageCount Then Set orngBreak = oRng.Paragraphs(1).Range.Previous.Paragraphs(1).Range orngBreak.Collapse 1 orngBreak.InsertBreak (7) End If Next lCount oApp.Visible = True End Sub




Reply With Quote