Excel text to Word Document, controlling the Page Breaks

tlawson1

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

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

tlawson1

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

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,723
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
 

tlawson1

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

tlawson1

Board Regular
Joined
Jun 15, 2003
Messages
68
Also posted at
 

tlawson1

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

Watch MrExcel Video

Forum statistics

Threads
1,122,472
Messages
5,596,354
Members
414,060
Latest member
hermanseck

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
Top