Consulting

Results 1 to 5 of 5

Thread: Excel text to Word Document, controlling the Page Breaks

  1. #1

    Excel text to Word Document, controlling the Page Breaks

    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

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    Excel text to Word Document, controlling the Page Breaks | MrExcel Message Board
    Please review the cross posting guidelines at both forums. Dave

  3. #3

  4. #4
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Many thanks Graham. This has solved my problem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •