none
removing Word blank pages RRS feed

  • Question

  • I have a Word document generated by another tool that, when printed (and in print preview), inserts blank pages throughout the document. Can I remove them at runtime using VSTO?

    Also, the page orientation switches thoughout the document, but when printing, the orientation may not return to portrait after a landscape page. Any help with this would be appreciated, otherwise, I will just make the whole document landscape.

    Thanks.

    Sunday, March 25, 2007 2:17 AM

Answers

  • <<getting closer, seems like the blank pages have a carriage return only, when I try to delete them as below, I get a  'can't edit range' error>>

     

    If these pages were inserted using "manual page breaks", then they consist mainly of a page break character (Asc(12)). Look at such a document in Word, with all non-printing characters displayed (click the "backwards P" in the toolbar). You should see a ----- Page Break ----- at the end of the preceding page. This is basically the thing you need to delete.

     

    If the page breaks were inserted using "Next Page section breaks" (you mention changes of orientation, so it's a possibility), then this would also be an Asc(12) character. But if you start deleting section breaks you could really make a mess of the document formatting (orientation, headers, footers).

     

    Likely, the paragraph mark you're trying to delete is linked to the page break, and is thus protected. You may only be able to delete it by also deleting the break.

     

    An alternate approach you might try, since these are "blank pages", would be to use Find/Replace to search the text ^b and replace it with nothing. You can test this in the UI before trying to program it to see if it gives you the desired result or if it "mucks up" the document.

    Wednesday, April 4, 2007 2:31 PM
    Moderator

All replies

  • Yes, you can do this using VSTO.  Handle the events that get fired before the printing or previewing happens, and then use the object model to achieve the removal of blank pages, formatting etc.

    HTH.

    Thursday, March 29, 2007 4:59 PM
  • Thanks for the response. Being totally unfamiliar with Word object model, but familiar with .net in general, I was hoping for something like:

    psuedocode - if myDocument.pages(i).content.length = 0 then delete pages (i) - but that obviously won't work.

    How do I get the page content length?

    Thursday, March 29, 2007 5:20 PM
  • <

    psuedocode - if myDocument.pages(i).content.length = 0 then delete pages (i) - but that obviously won't work.

    How do I get the page content length?>>

     

    It should work by using the old, built-in \Page bookmark. Something like this (VB pseudocode):

     

    Dim rng as Word.Range

    Dim doc as Word.Document

    doc = Me  'VSTO document project

    rng = doc.Bookmarks("\Page").Range

    rng.Delete()

     

    Tuesday, April 3, 2007 1:56 PM
    Moderator
  • <<Also, the page orientation switches thoughout the document, but when printing, the orientation may not return to portrait after a landscape page. Any help with this would be appreciated, otherwise, I will just make the whole document landscape.>>

     

    Hard to be certain about this, but I'm guessing this may be a problem with the section breaks. Word has to have a section break before and after each page orientation change (page orientation is a property of the section object). Is it possible that the section break(s) in question is being deleted?

    Tuesday, April 3, 2007 1:58 PM
    Moderator
  • Yes, it appears it could be the section breaks. The blank pages seem to follow the section breaks in most cases. I'm not deleting the section breaks, I've tried setting the range to a section, using range.moveend(char,-1), and deleting that character, and it works in some cases.

     

    Tuesday, April 3, 2007 2:12 PM
  • getting closer, seems like the blank pages have a carriage return only, when I try to delete them as below, I get a  'can't edit range' error:

     

    Code Snippet

    Dim r As Word.Range

    r = Me.Range

    Dim pages As Integer = r.Information(Word.WdInformation.wdNumberOfPagesInDocument)

    For page As Integer = 2 To pages

    Dim r2 As Word.Range = r.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, page)

    r2.Expand()

    'MessageBox.Show(String.Format("Page {0}: range start: {1}; range end: {2}.", page, r2.Start, r2.End))

    If Asc(r2.Text) = 13 Then

    'MessageBox.Show(String.Format("Page {0} should be deleted.", page))

    r2.Delete()

    End If

     

     

     

    Tuesday, April 3, 2007 7:50 PM
  • <<getting closer, seems like the blank pages have a carriage return only, when I try to delete them as below, I get a  'can't edit range' error>>

     

    If these pages were inserted using "manual page breaks", then they consist mainly of a page break character (Asc(12)). Look at such a document in Word, with all non-printing characters displayed (click the "backwards P" in the toolbar). You should see a ----- Page Break ----- at the end of the preceding page. This is basically the thing you need to delete.

     

    If the page breaks were inserted using "Next Page section breaks" (you mention changes of orientation, so it's a possibility), then this would also be an Asc(12) character. But if you start deleting section breaks you could really make a mess of the document formatting (orientation, headers, footers).

     

    Likely, the paragraph mark you're trying to delete is linked to the page break, and is thus protected. You may only be able to delete it by also deleting the break.

     

    An alternate approach you might try, since these are "blank pages", would be to use Find/Replace to search the text ^b and replace it with nothing. You can test this in the UI before trying to program it to see if it gives you the desired result or if it "mucks up" the document.

    Wednesday, April 4, 2007 2:31 PM
    Moderator