none
Remove Blank Pages from Docx using word interop RRS feed

  • Question

  • Hi,

    Is there is any way to delete all blankpages from a word document (Docx) using word interop.


    -Rajesh A.

    Thursday, July 26, 2012 5:56 AM

Answers

  • Hi Rajesh,

    In VBA, the code could be based on a wildcard Find/Replace like:

    Sub Demo()
    With ActiveDocument.Content.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "^12[^12^13 ]{1,}"
      .Replacement.Text = "^12"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    End Sub

    The above code also caters for the possibility that the 'blank' pages have paragraph breaks and/or spaces on them. 


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Thursday, July 26, 2012 12:36 PM

All replies

  • Yes, but the method partly depends on how the blank pages were created/came to be blank.

    For example, if they are created via manual page breaks, you could use Find/Replace to replace all consecutive manual page breaks with single manual page breaks.

    Conversely, if Section breaks are involved, you'll have issues of header & footer content, page margins & orientation to consider as part of the mix. Odd/Even Next Page Section breaks add to the complexity.

    Given the foregoing issues, you'll need to provide more details about your blank pages before anyone can give specific advice.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, July 26, 2012 6:42 AM
  • Hi Macropod,

    Thanks for your reply.Actually those blank pages were created by page Breaks.


    -Rajesh A.

    Thursday, July 26, 2012 9:07 AM
  • Hi Rajesh,

    In VBA, the code could be based on a wildcard Find/Replace like:

    Sub Demo()
    With ActiveDocument.Content.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "^12[^12^13 ]{1,}"
      .Replacement.Text = "^12"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    End Sub

    The above code also caters for the possibility that the 'blank' pages have paragraph breaks and/or spaces on them. 


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Thursday, July 26, 2012 12:36 PM