none
Adding "PAGE LEFT INTENTIONALLY BLANK" on Word created blank pages RRS feed

  • Question

  • Hello everyone I'm back already!

    I have a software requirement to insert "PAGE LEFT INTENTIONALLY BLANK" on Word created blank pages.  These are the magical pages Word creates when you print when a section break causes to odd pages to come together.  My logic mostly works but starts to fail miserably when the document is large (not sure if that really has anything to do with it). 

    If anyone can spot a problem with my "hack" please chime in :)

    Here is what is somewhat working:

     internal static List<int> SetBlankPageText(Document document)
            {
                Word.Range range = document.Content.Duplicate;
                List<int> pages = BlankPages(document);
    
                document.UndoClear();
                document.Application.UndoRecord.StartCustomRecord("BLANKPAGES");
                foreach (int page in pages)
                {
                    range = range.GoTo(Word.WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, page, Type.Missing);
                    document.Application.Selection.SetRange(range.Start, range.End);
                    document.Application.Selection.MoveLeft(WdUnits.wdCharacter, 1);
                    //document.Application.Selection.InsertBreak(WdBreakType.wdPageBreak);
                    document.Application.Selection.TypeText("THIS PAGE INTENTIONALLY LEFT BLANK");
                    document.Application.Selection.Range.ListFormat.RemoveNumbers(WdNumberType.wdNumberParagraph);
                    document.Application.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    document.Application.Selection.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                    document.Application.Selection.ParagraphFormat.LineSpacing = document.Application.InchesToPoints(4.8f);
                }
                document.Application.UndoRecord.EndCustomRecord();
                return pages;
            }
    
     internal static List<int> BlankPages(Document document)
            {
                Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                Word.Range range = document.Content.Duplicate;
                int TotalPagesInDocument = document.ComputeStatistics(stat, Type.Missing);
                List<int> BlankPages = new List<int>();
                List<int> starts = new List<int>();
                for (int page = 1; page <= TotalPagesInDocument; page++)
                {
                    Range pageRange = range.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, page);
                    if (starts.Contains(pageRange.Start))
                        BlankPages.Add(page - 1);
                    else
                        starts.Add(pageRange.Start);
                }
                return BlankPages;
            }


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Wednesday, March 26, 2014 11:11 PM

Answers

  • You can't create nested fields that way. For starters, all the braces (i.e. '{ }') need to be parts of fields inserted in their own right. There are two ways of addressing this requirement:
    1. Re-write the code so that it creates the required nested fields directly; or:
    2. Insert the textual representation of the field code where you want it, then convert that to a working field.

    The VBA code for (1) is:

    Sub InsertBlankPageField()
    Dim Sctn As Section, Rng As Range
    ActiveWindow.View.ShowFieldCodes = True
    With ActiveDocument
      For Each Sctn In .Sections
        With Sctn.Range
          If .Characters.Last.Previous <> vbCr Then _
            .Characters.Last.InsertBefore vbCr
          Set Rng = .Characters.Last.Previous
          Rng.Collapse wdCollapseStart
          .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
            PreserveFormatting:=False, Text:="IF = 1"
          Set Rng = .Fields(1).Code
          Rng.Start = Rng.Start + 3
          Rng.Collapse wdCollapseStart
          .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
            PreserveFormatting:=False, Text:="=MOD(,2)"
          Set Rng = .Fields(2).Code
          Rng.Start = Rng.Start + 6
          Rng.Collapse wdCollapseStart
          .Fields.Add Range:=Rng, Type:=wdFieldPage, _
          PreserveFormatting:=False
          Set Rng = .Fields(1).Code
          Rng.End = Rng.End
          Rng.Collapse wdCollapseEnd
          .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
            PreserveFormatting:=False, Text:="QUOTE 12 " & Chr(34) & _
            "This page intentionally left blank" & Chr(34)
          .Fields.Update
        End With
      Next
    End With
    ActiveWindow.View.ShowFieldCodes = False
    End Sub

    Code for (2) can be adapted from: http://www.gmayor.com/export_field.htm#TextToField


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Marvin_Guo Wednesday, April 2, 2014 11:42 AM
    Thursday, March 27, 2014 9:51 AM

All replies

  • I'm also trying to programmatically add a field code I came across from Paul Edstein in an old post.  Nothing is happening though.  I admittedly am not good with Word field formulas so I'm not certain where the problem is.

    Here is the code:

      internal static void SetBlankPageText2(Document document)
            {
                document.UndoClear();
                document.Application.UndoRecord.StartCustomRecord("BLANKPAGES");
    
                foreach (Section section in document.Sections)
                {
                    if (section.Range.Start > 0)
                    {
                        Range range = document.Range(section.Range.Start - 1, section.Range.Start - 1);
                        document.Fields.Add(range, WdFieldType.wdFieldFormula, "IF{=MOD({PAGE},2)}= 1 \"{QUOTE 12 \"This page intentionally left blank\"}");
                    }
                }
                document.Fields.Update();
                document.Application.UndoRecord.EndCustomRecord();
            }

    The idea here is to perform an "Undo" after printing to remove the text or fields so the document doesn't have that text lurking about.


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Thursday, March 27, 2014 12:01 AM
  • You can't create nested fields that way. For starters, all the braces (i.e. '{ }') need to be parts of fields inserted in their own right. There are two ways of addressing this requirement:
    1. Re-write the code so that it creates the required nested fields directly; or:
    2. Insert the textual representation of the field code where you want it, then convert that to a working field.

    The VBA code for (1) is:

    Sub InsertBlankPageField()
    Dim Sctn As Section, Rng As Range
    ActiveWindow.View.ShowFieldCodes = True
    With ActiveDocument
      For Each Sctn In .Sections
        With Sctn.Range
          If .Characters.Last.Previous <> vbCr Then _
            .Characters.Last.InsertBefore vbCr
          Set Rng = .Characters.Last.Previous
          Rng.Collapse wdCollapseStart
          .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
            PreserveFormatting:=False, Text:="IF = 1"
          Set Rng = .Fields(1).Code
          Rng.Start = Rng.Start + 3
          Rng.Collapse wdCollapseStart
          .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
            PreserveFormatting:=False, Text:="=MOD(,2)"
          Set Rng = .Fields(2).Code
          Rng.Start = Rng.Start + 6
          Rng.Collapse wdCollapseStart
          .Fields.Add Range:=Rng, Type:=wdFieldPage, _
          PreserveFormatting:=False
          Set Rng = .Fields(1).Code
          Rng.End = Rng.End
          Rng.Collapse wdCollapseEnd
          .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
            PreserveFormatting:=False, Text:="QUOTE 12 " & Chr(34) & _
            "This page intentionally left blank" & Chr(34)
          .Fields.Update
        End With
      Next
    End With
    ActiveWindow.View.ShowFieldCodes = False
    End Sub

    Code for (2) can be adapted from: http://www.gmayor.com/export_field.htm#TextToField


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Marvin_Guo Wednesday, April 2, 2014 11:42 AM
    Thursday, March 27, 2014 9:51 AM
  • Hello Paul,

    Sorry for the delay and thanks for your help.
    Your script (VBA 1) worked on a great deal of pages but some pages are still blank.  I'm investigating why that is happening and then I'm adapting it to C#.  I'll post my results when I figure out what is happening.


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Sunday, March 30, 2014 6:08 PM
  • Your script (VBA 1) worked on a great deal of pages but some pages are still blank.  I'm investigating why that is happening and then I'm adapting it to C#.  I'll post my results when I figure out what is happening.


    Perhaps because those blank pages were already there... The code doesn't remove what's already in the document. The other possibility is that the underlying page numbering is putting odd/even-numbered pages on the wrong pages.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 30, 2014 9:22 PM
  • If the document uses odd or even section breaks and Word inserts a blank page as a consequence of that, those pages are genuinely blank and I don't think you get the opportunity to put anything on them at all.

    Peter Jamieson

    Monday, March 31, 2014 8:55 AM
  • Hello All,

    Thank you for your help.  I'll go with Marvin's recommendation and accept Paul's answer even though it didn't solve my problem but perhaps this isn't solvable but points are deserved.

    I think I'm going to go with a print to pdf then manipulate the pdf.  Ugly as $#%* but I'm not getting anywhere with this and need some solution.

    Thanks again, happy coding!


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Wednesday, April 2, 2014 3:23 PM