none
Extra blank pages when creating Word 2007 documents. RRS feed

  • Question

  • I have written an application that uses Word automation to create reports. The reports are created by concatenating (Range.InsertFile) document segments, each of which is stored as a separate .docx file. The .docx files consist of either text with bookmarks for insertion points, or tables which get populated with the appropriate data. Each segment ends with a Page Break, so that each part begins on a fresh page.

    This all works nicely, but I occasionally get the odd blank page in the middle of the report. This is due to a populated document segment being just long enough to cause Word to start a new page, but not actually put anything visible on it (e.g. an End of Paragraph marker spills over to the next page). Since each segment ends with a Page Break anyway, this causes an extra blank page to appear.

    Is there any way I can prevent this, or maybe detect that the Page Break is not needed?

    Tuesday, March 5, 2013 9:59 AM

Answers

  •  
    Thanks Paul, I've had a quick play and it looks like it will solve some of my problems.
     
    However, my document has an automatic Table Of Contents object which is just long enough to spill over to a new page (only the End Of Paragraph marker inside the 'Update Table' rectangle appears on the new page). When I run your script, that blank page disappears for a moment, but then (I guess) the Table Of Contents updates itself again and the blank page is re-added :-(
     
     
     
    You can format the problem paragraph mark as hidden text and/or in a white font color, 1 point size, with zero spacing before and after, and the line spacing set to exactly 1 point.

    Stefan Blom, Microsoft Word MVP
    Tuesday, March 5, 2013 11:56 PM
  • There is a limit to what can be achieved here. Since everything between the TOC and the page break/section break following it has been deleted, and the TOC necessarily terminates in a paragraph break, it will re-appear no matter what you do. In that regard, Stefan's suggestion is no better. I suppose I could have written the code to test whether the paragraph to be deleted was part of a field, so as to not raise false hopes but, as you can see, Word helpfully puts them back anyway.

    About all you can do to eliminate the unwanted post-TOC page is to play around with the TOC Styles or whatever precedes the TOC to reduce the amount of space required. Although you'll be able to routinely apply certain fixes (and perhaps some could become part of the default format), you'll still end up having to deal with some scenarios on a case-by-case basis. For example, the fix for a TOC with nothing more than an empty following page will be different to the fix for a TOC with only one or two entries on the last page. For the latter, you might decide to shrink or expand the TOC spacing to get a pleasing effect.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, March 6, 2013 2:43 AM
  • There is a limit to what can be achieved here. Since everything between the TOC and the page break/section break following it has been deleted, and the TOC necessarily terminates in a paragraph break, it will re-appear no matter what you do. In that regard, Stefan's suggestion is no better.


     
     
    Good point, there are cases we can't deal with generally... If the "excessive" paragraph mark was created by the TOC itself, then (as Paul says) changing the formatting of TOC styles will be the only solution (the appropriate action depends on the document). However, I assumed this was about a blank paragraph following the TOC field, in which case formatting the paragraph mark should help.

    Stefan Blom, Microsoft Word MVP
    Wednesday, March 6, 2013 12:56 PM

All replies

  • After creating the document, you can use code like the following to clean up any 'white space' before manual page breaks and Section breaks and at the end of the document.

    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[ ^13^l^t]{1,}(^12)"
        .Replacement.Text = "\1"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
      End With
      While .Characters.Last.Previous.Text Like "[ " & vbTab & Chr(11) & vbCr & "]"
        .Characters.Last.Previous.Text = vbNullString
      Wend
    End With

    That should be sufficient to resolve the issue. Do note, though, that you cannot delete an empty paragraph after a table that concludes the document. If that causes a new page, the standard solution is to reduce its before/after spacing to 0, set the point size to 1 and format it as hidden.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 5, 2013 10:52 AM
  • Thanks Paul, I've had a quick play and it looks like it will solve some of my problems.

    However, my document has an automatic Table Of Contents object which is just long enough to spill over to a new page (only the End Of Paragraph marker inside the 'Update Table' rectangle appears on the new page). When I run your script, that blank page disappears for a moment, but then (I guess) the Table Of Contents updates itself again and the blank page is re-added :-(

    Tuesday, March 5, 2013 11:31 AM
  •  
    Thanks Paul, I've had a quick play and it looks like it will solve some of my problems.
     
    However, my document has an automatic Table Of Contents object which is just long enough to spill over to a new page (only the End Of Paragraph marker inside the 'Update Table' rectangle appears on the new page). When I run your script, that blank page disappears for a moment, but then (I guess) the Table Of Contents updates itself again and the blank page is re-added :-(
     
     
     
    You can format the problem paragraph mark as hidden text and/or in a white font color, 1 point size, with zero spacing before and after, and the line spacing set to exactly 1 point.

    Stefan Blom, Microsoft Word MVP
    Tuesday, March 5, 2013 11:56 PM
  • There is a limit to what can be achieved here. Since everything between the TOC and the page break/section break following it has been deleted, and the TOC necessarily terminates in a paragraph break, it will re-appear no matter what you do. In that regard, Stefan's suggestion is no better. I suppose I could have written the code to test whether the paragraph to be deleted was part of a field, so as to not raise false hopes but, as you can see, Word helpfully puts them back anyway.

    About all you can do to eliminate the unwanted post-TOC page is to play around with the TOC Styles or whatever precedes the TOC to reduce the amount of space required. Although you'll be able to routinely apply certain fixes (and perhaps some could become part of the default format), you'll still end up having to deal with some scenarios on a case-by-case basis. For example, the fix for a TOC with nothing more than an empty following page will be different to the fix for a TOC with only one or two entries on the last page. For the latter, you might decide to shrink or expand the TOC spacing to get a pleasing effect.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, March 6, 2013 2:43 AM
  • There is a limit to what can be achieved here. Since everything between the TOC and the page break/section break following it has been deleted, and the TOC necessarily terminates in a paragraph break, it will re-appear no matter what you do. In that regard, Stefan's suggestion is no better.


     
     
    Good point, there are cases we can't deal with generally... If the "excessive" paragraph mark was created by the TOC itself, then (as Paul says) changing the formatting of TOC styles will be the only solution (the appropriate action depends on the document). However, I assumed this was about a blank paragraph following the TOC field, in which case formatting the paragraph mark should help.

    Stefan Blom, Microsoft Word MVP
    Wednesday, March 6, 2013 12:56 PM
  • Hi Somnolent,

    I temporarily marked the replies as answers. You can unmark them if they provide no help.

    Please feel free to let us know if you need any further help.

    Have a nice day.


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 2:58 AM
    Moderator