Hi all,


I am using Late Binding to send some text to a Word document. If my text is spread over 2 pages then I need to insert a Page Break


The reason for this is that my finished routine will need different text at the start of the next page


For testing purposes, it is all about inserting a Page Break at the correct line number


My routine works for the first page but then fails. The page and line numbering code needs altering but I am stuck.


Can anyone help?

Public oApp As Object    'application
Public oDoc As Object    'document
Public oSelection As Object    'Create a Selection object


Sub Test()
Dim lCount As Long
Dim lLineNumber As Long
Dim lPageCount As Long
Dim sText As String
Dim lWdPageNumber As Long


Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add
Set oSelection = oApp.Selection


For lCount = 1 To 60


'get current line number and page number before inserting text
lLineNumber = oSelection.Information(10)  'wdFirstCharacterLineNumber
lPageCount = oSelection.Information(3)    'wdActiveEndPageNumber


sText = "Text to test" & lCount & vbNewLine


With oSelection
.TypeParagraph
.TypeText (sText)


'word current page number after inserting text
lWdPageNumber = oSelection.Range.Information(3)    'wdActiveEndPageNumber


If lWdPageNumber > lPageCount Then
'goto previous page
.GoTo What:=(1), Which:=(3)    '(What=wdGoToPage,Which=wdGoToPrevious, Count=Page number)
                
'goto line number before text was inserted
.GoTo What:=(3), Which:=(1), Count:=lLineNumber    '(What=wdGoToLine, Which=wdGoToAbsolute, Count=Line number)
.InsertBreak (7)    'wdPageBreak
.EndKey Unit:=6
End If
End With
Next lCount
oApp.Visible = True
End Sub