PDA

View Full Version : Excel text to Word Document, controlling the Page Breaks



metalwork10
12-12-2020, 02:52 PM
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

Dave
12-13-2020, 12:54 PM
Excel text to Word Document, controlling the Page Breaks | MrExcel Message Board (https://www.mrexcel.com/board/threads/excel-text-to-word-document-controlling-the-page-breaks.1155783/)
Please review the cross posting guidelines at both forums. Dave

metalwork10
12-13-2020, 02:06 PM
Also posted at
https://www.mrexcel.com/board/threads/excel-text-to-word-document-controlling-the-page-breaks.1155783/#post-5603819

gmayor
12-13-2020, 10:55 PM
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

metalwork10
12-14-2020, 01:06 AM
Many thanks Graham. This has solved my problem:yes