Page-break in Words while running in Excel

Important Notice


Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

  • Hi,
    Please, can anyone help me with the shortcomings mentioned here about a "page-break" instruction in a Words document in a code when running in Excel.


    In addition, the normal code in Words for MoveUp" does not work, I want to get up into an imported table with data and adjust the characteristics of the table in order to fit the page in Words.


    Thanks in advance for kind assistance
    / bjorn


    This is my very first attempt to seek advice in programming VBA.
    Background: XP professional, Office 2000

    The context & the problem :

    Running a long program in Excel with many macros / coded subs in background.
    The program will result in a lot of reports, typically of 5-25 pages each & with a lot of conditional text created in excel macros / VBA
    I want (must) get print out in Words as such printout is smoother.
    Everything is - be the end of a lot of work - OK, except to make a page-break in the excel routine that runs what to be done in the created word document.
    Some instructions are OK, for example to rearrange margins, others, for example pagehead and pagefoot does not work, but WORST:
    In words, the "instruction" for page break is - .InsertBreak Type:=wdPageBreak - to be put within With wrd (= stands for Words..) and With .selection.
    In words, it works, but when running from Excel, it does NOT work.

    The code under Excel used for this small program sequence is found below
    ' Open a document
    Set wrd = GetObject(, "Word.Application"):


    With wrd:
    With .Selection
    .TypeParagraph
    .TypeParagraph
    .TypeText Text:="This is page 1"
    .TypeParagraph


    .TypeParagraph
    '.Collapse Direction:=wdCollapseEnd
    '.InsertBreak Type:=wdPageBreak ' tried - did not work

    '.InsertBreak Type:=wdSectionBreakContinuous, ' tried - did not work
    .TypeParagraph
    .TypeParagraph
    .TypeText Text:="Some text on page 2"
    .TypeParagraph
    .Sections.Add ' this works, but text instructions after this command IS NOT placed after a page break
    ' .MoveEndUntil Cset:="a ", Count:=wdForward, tried.... did not work
    ' .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    .TypeText Text:="Here should be text, for example to state that we are now on page 2..."
    End With
    End With
    Stop


    Is it possible to have some help on this (trivial ??) matter ?
    Regards Bjorn

  • I use this (but am new to this XL/Word interaction thingy)......



    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    ':
    ':
    ':
    ':
    'lots of other stuff here, like:
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Alignment = wdAlignParagraphLeft
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Text = "Some statement...."
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
    ':
    ':
    ' If I need a page-break:


    With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
    .InsertParagraphBefore
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    .InsertParagraphAfter
    End With



    'Don't know if this is of any help..or indeed if you query was posted several years ago :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!