none
Programattically delete "blank" page at the end of a word document? RRS feed

  • Question

  • So in Word 2003, in a document created from a (custom letter) template with a table in the second page footer, when i attempt to add an envelope and then delete everything but the envelope, i always end up with an extra blank page after the envelope.

    This appears to be a noted problem:

    http://wordfaqs.mvps.org/BlankPage.htm

    "If there is a table at the very end of your document, Word will insist on having an empty paragraph after the table. You will not be able to delete this paragraph because the paragraph holds the formatting for the entire document (or the last section if there is more than one). The end-of-table marker cannot hold this information because it contains information about the formatting of the table.

    If your table completely fills the “last” page, the empty paragraph will result in a “blank” page following it. Although you cannot remove this paragraph, you can make it disappear. "

    I couldn't quite find a solution elsewhere: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/a53cc959-1462-403a-af5d-bdfde5113b11

    So any thoughts on this?

    I'm currently using this code (Visual Studio 2010 Professional):

     Dim intKeepSections As Integer = 0
                Dim intSections As Integer = 0
                ' Find out number of sections in document
                intSections = mobjApplication.ActiveDocument.Sections.Count
                ' is there an envelope?
                If EnvelopeExists() Then
                    intKeepSections = intKeepSections + 1
                End If
                ' is there a label?
                If LabelsExist() Then
                    intKeepSections = intKeepSections + 1
                End If
                ' remove all the other sections
                mobjApplication.ActiveDocument.Sections(intKeepSections).Range.Select()
                mobjApplication.Selection.MoveRight()
                mobjApplication.Selection.MoveLeft()
                mobjApplication.Selection.InsertBreak(WD.WdBreakType.wdSectionBreakContinuous)
                mobjApplication.Selection.Extend()
                mobjApplication.Selection.MoveEnd(WD.WdUnits.wdSection, intSections + 1 - intKeepSections)
                mobjApplication.Selection.Delete()
                '' Clear all Headers
                'For intSections = intKeepSections To mobjApplication.ActiveDocument.Sections.Count
                '    mobjApplication.ActiveDocument.Sections(intSections).Headers(WD.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Select()
                '    mobjApplication.Selection.Delete()
                '    mobjApplication.ActiveDocument.Sections(intSections).Footers(WD.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Select()
                '    mobjApplication.Selection.Delete()
                'Next intSections

    With the commented out bit as an attempt at the footer directly.


    • Edited by ZappyZane Wednesday, May 2, 2012 1:49 PM
    Wednesday, May 2, 2012 1:48 PM

Answers

  • You can use a macro to format the paragraph mark following the table as hidden and/or in 1 pt font size, 1 pt line spacing, zero spacing before/after.

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by ZappyZane Thursday, May 3, 2012 9:57 AM
    Wednesday, May 2, 2012 5:51 PM
  • Yes my first link mentioned this.  I'd tried it non-programitcally (before starting this thread) and it hadnt worked, but had a go in code just now and it worked - doh!

    For anyone else that runs into this.  I had to jiggle the linespacing in the last three lines or it'd error:

        With mobjApplication.Selection
                    .ParagraphFormat.SpaceBefore = 0
                    .ParagraphFormat.SpaceAfter = 0
                    .ParagraphFormat.LineSpacing = WD.WdLineSpacing.wdLineSpaceAtLeast
                    .ParagraphFormat.LineSpacingRule = Microsoft.Office.Word.WdLineSpacing.wdLineSpaceExactly
                    .ParagraphFormat.LineSpacing = 1
                End With

    • Marked as answer by ZappyZane Thursday, May 3, 2012 9:56 AM
    Thursday, May 3, 2012 9:56 AM

All replies

  • You can use a macro to format the paragraph mark following the table as hidden and/or in 1 pt font size, 1 pt line spacing, zero spacing before/after.

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by ZappyZane Thursday, May 3, 2012 9:57 AM
    Wednesday, May 2, 2012 5:51 PM
  • Yes my first link mentioned this.  I'd tried it non-programitcally (before starting this thread) and it hadnt worked, but had a go in code just now and it worked - doh!

    For anyone else that runs into this.  I had to jiggle the linespacing in the last three lines or it'd error:

        With mobjApplication.Selection
                    .ParagraphFormat.SpaceBefore = 0
                    .ParagraphFormat.SpaceAfter = 0
                    .ParagraphFormat.LineSpacing = WD.WdLineSpacing.wdLineSpaceAtLeast
                    .ParagraphFormat.LineSpacingRule = Microsoft.Office.Word.WdLineSpacing.wdLineSpaceExactly
                    .ParagraphFormat.LineSpacing = 1
                End With

    • Marked as answer by ZappyZane Thursday, May 3, 2012 9:56 AM
    Thursday, May 3, 2012 9:56 AM
  • When doing this in the user interface, having nonprinting marks displayed (use the ¶ button) helps. In code, it is usually safer not to depend on the selection object.

    Stefan Blom, Microsoft Word MVP
    Thursday, May 3, 2012 6:42 PM