Excel text to Word Document, controlling the Page Breaks

tlawson1

Board Regular
Joined
Jun 15, 2003
Messages
69
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?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I must have done something wrong as my code has not been posted so here it is.
VBA Code:
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
 
Upvote 0
Different text at the start of the page... maybe just use/adjust the header? U could just loop though all the paragraphs in the document and evaluate each one to see what page it is on. I'm guessing there's an easier solution though. HTH. Dave
 
Upvote 0
The Header already contains information so I can't really use that. The text I am doing is only a small part of a long report. I can find the Page I'm on I just don't seem to be able to reference the line number of the document. As far as I know MS Word starts counting a line number from the beginning of a document.
 
Upvote 0
Graham has posted a solution at

VBA Code:
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
 
Upvote 0
Solution

Forum statistics

Threads
1,213,487
Messages
6,113,943
Members
448,534
Latest member
benefuexx

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