Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2016, 01:50 AM
Cov_ATC Cov_ATC is offline Word VBA - "Page left Blank" between Sections to make all Sections start on even page Windows 7 64bit Word VBA - "Page left Blank" between Sections to make all Sections start on even page Office 2013
Novice
Word VBA - "Page left Blank" between Sections to make all Sections start on even page
 
Join Date: Oct 2016
Posts: 5
Cov_ATC is on a distinguished road
Default Word VBA - "Page left Blank" between Sections to make all Sections start on even page

Hi,



I am editing a huge Word Doc for my airport, which has many sections.
However some sections started on Odd pages.

I found online a Word VBA code (image 3, attached), and it seems to be the only one out there, that does the job of adding a blank page to those Sections starting on odd pages (as seen in Image 1, attached).
The code works brilliantly when executed (as seen in Image 2, attached).

Our problem is that what the code only seems to be doing, is shifting the PageBreak and SectionBreak prior to the new sections that start on Odd pages (as seen in image 1) to a new page by adding another PageBreak, thus also Shifting the Section that started on Odd page to a new page, this time an Even page. (as seen on Image 2).

However, we would like that that blank page that this effectively created has written something like "Page Left Blank" with the following formatting: Centered on the whole page, Bold, Grey Colour, font Size 20, Font type Arial.

Could you help us in improving the code, or have another code to go along side it to do the job.

Kind regards,

Chris C.
Reply With Quote
  #2  
Old 10-11-2016, 04:52 AM
gmayor's Avatar
gmayor gmayor is offline Word VBA - "Page left Blank" between Sections to make all Sections start on even page Windows 10 Word VBA - "Page left Blank" between Sections to make all Sections start on even page Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

create a watermark using one of the built-in watermarks in a new blank document. Open the header and select the Word art. Change the text to 'Page left blank' then while it remains selected ALT+F3 and save it as an autotext entry called 'Blank Page'. The following, based on your code, should then create the blank pages and add the autotext across each.

Code:
Sub PageBreakSectionSeparator()
Dim Sect As Section
Dim iSec As Integer
Dim oRng As Range
Dim iValue As Integer
    With ActiveDocument
        On Error Resume Next
        ' go through each section (except for the last one)
        For iSec = 1 To .Sections.Count - 1
            ' create a range object at the start of the section
            Set oRng = .Sections(iSec).Range
            oRng.Collapse wdCollapseStart
            ' insert a sectionpages field
            .Fields.Add Range:=oRng, Type:=wdFieldSectionPages
            ' divide the sectionpages field by 2
            ' if it gives a zero as the remainder, then
            ' you have an even number of pages in the section,
            ' which is what you want with an odd section page break
            If (.Sections(iSec).Range.Fields(1).Result Mod 2) <> 0 Then
                ' if you have an odd number of pages, then insert
                ' a page break before the section's section break
                Set oRng = .Sections(iSec).Range
                With oRng
                    .Collapse direction:=wdCollapseEnd
                    .MoveEnd unit:=wdCharacter, Count:=-1
                    'Ensure the insertion point is at the end of the text
                    .MoveEndWhile Chr(13) & Chr(32), wdBackward
                    'Insert a page break
                    .InsertBreak Type:=wdPageBreak
                    'Move the insertion point to the blank page
                    .End = .Sections(iSec).Range.End - 1
                    .Collapse direction:=wdCollapseEnd
                    'Insert a building block with the required notification
                    NormalTemplate.BuildingBlockEntries("BlankPage").Insert _
                            Where:=oRng
                End With
            End If
            ' remove the sectionpages field that was added
            .Sections(iSec).Range.Fields(1).Delete
        Next iSec
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-11-2016, 04:58 AM
gmayor's Avatar
gmayor gmayor is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 10 Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

On reflection the field should not be necessary try the following instead
Code:
Sub PageBreakSectionSeparator()
Dim Sect As Section
Dim iSec As Integer
Dim oRng As Range
Dim iValue As Integer
    With ActiveDocument
        On Error Resume Next
        ' go through each section (except for the last one)
        For iSec = 1 To .Sections.Count - 1
            ' create a range object at the start of the section
            Set oRng = .Sections(iSec).Range
            oRng.Collapse wdCollapseStart
            If .Sections(iSec).Range.Information(wdActiveEndPageNumber) Mod 2 <> 0 Then
                ' if you have an odd number of pages, then insert
                ' a page break before the section's section break
                Set oRng = .Sections(iSec).Range
                With oRng
                    .Collapse direction:=wdCollapseEnd
                    .MoveEnd unit:=wdCharacter, Count:=-1
                    'Ensure the insertion point is at the end of the text
                    .MoveEndWhile Chr(13) & Chr(32), wdBackward
                    'Insert a page break
                    .InsertBreak Type:=wdPageBreak
                    'Move the insertion point to the blank page
                    .End = .Sections(iSec).Range.End - 1
                    .Collapse direction:=wdCollapseEnd
                    'Insert a building block with the required notification
                    NormalTemplate.BuildingBlockEntries("BlankPage").Insert _
                            Where:=oRng
                End With
            End If
        Next iSec
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 10-19-2016, 02:15 AM
Cov_ATC Cov_ATC is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2013
Novice
Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page
 
Join Date: Oct 2016
Posts: 5
Cov_ATC is on a distinguished road
Default

Thank you very much for your help,
I apologise for late reply,
I was on leave.
I will try the code asap and let you know the outcome.

Regards,

Cov_ATC









Quote:
Originally Posted by gmayor View Post
On reflection the field should not be necessary try the following instead
Code:
Sub PageBreakSectionSeparator()
Dim Sect As Section
Dim iSec As Integer
Dim oRng As Range
Dim iValue As Integer
    With ActiveDocument
        On Error Resume Next
        ' go through each section (except for the last one)
        For iSec = 1 To .Sections.Count - 1
            ' create a range object at the start of the section
            Set oRng = .Sections(iSec).Range
            oRng.Collapse wdCollapseStart
            If .Sections(iSec).Range.Information(wdActiveEndPageNumber) Mod 2 <> 0 Then
                ' if you have an odd number of pages, then insert
                ' a page break before the section's section break
                Set oRng = .Sections(iSec).Range
                With oRng
                    .Collapse direction:=wdCollapseEnd
                    .MoveEnd unit:=wdCharacter, Count:=-1
                    'Ensure the insertion point is at the end of the text
                    .MoveEndWhile Chr(13) & Chr(32), wdBackward
                    'Insert a page break
                    .InsertBreak Type:=wdPageBreak
                    'Move the insertion point to the blank page
                    .End = .Sections(iSec).Range.End - 1
                    .Collapse direction:=wdCollapseEnd
                    'Insert a building block with the required notification
                    NormalTemplate.BuildingBlockEntries("BlankPage").Insert _
                            Where:=oRng
                End With
            End If
        Next iSec
    End With
End Sub
Reply With Quote
  #5  
Old 10-19-2016, 06:18 PM
macropod's Avatar
macropod macropod is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You really don't need a macro for this. Not only that but using a macro can result in unwanted 'Page left blank' pages when you open or print the document using a different active printer driver.

All you need is a field, coded along the lines of:
{IF{=MOD({PAGE},2)}= 1 "{QUOTE 12}¶
This page intentionally left blank¶
"}
to automatically insert a blank page if the page on which they occur is an odd-numbered one.

To get the same effect for an even-numbered page, change the '1' to '0'.

Such a field would be used with ordinary 'Next Page' Section breaks to separate chapters and placed after the final '.' in each chapter (i.e. immediately before the 'Next Page' Section break).

Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. The spaces represented in the field construction are all required. Instead of the ¶ symbols shown in the examples, you should use real paragraph breaks. Simply format the 'This page intentionally left blank' with however much 'before' spacing you require and apply a centred alignment.

As a bonus, the field code solution obviates the need for the .docm format and any macro security/activation/running issues.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 10-20-2016, 06:45 AM
Cov_ATC Cov_ATC is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2013
Novice
Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page
 
Join Date: Oct 2016
Posts: 5
Cov_ATC is on a distinguished road
Default

Hi GMayor

Thank you for your help. I have managed to get it to work as you suggested.

HOWEVER, Macropod,

I now am really interested with your Field Code solution.
The only issue: I must then go through the whole document to input the code (with CTRL+F9 many times) to have the code added before all the SectionBreaks. The document is huge and since I can't "copy/Paste", I wondered if there were a way to VBA code the field codes, so that if I click on a button it will run a Sub / End Sub that will have the field code encoded in the Sub and have the vba code to add a sectionbreak?

for instance, something as such: (with the field code actually in a vba format)


Code:
Sub AddNextPageSectionBreakWithFieldCode()
 With Selection
   
    'Field code
    {IF{=MOD({PAGE},2)}= 1 "{QUOTE 12}¶
     This page intentionally left blank¶
    "}

    'Adds a new line
    .TypeParagraph

    'puts a Next Page SectionBreak
    .InsertBreak Type:=wdSectionBreakNextPage

 End With
End Sub
Kind regards,

Cov_ATC



Quote:
Originally Posted by macropod View Post
You really don't need a macro for this. Not only that but using a macro can result in unwanted 'Page left blank' pages when you open or print the document using a different active printer driver.

All you need is a field, coded along the lines of:
{IF{=MOD({PAGE},2)}= 1 "{QUOTE 12}¶
This page intentionally left blank¶
"}
to automatically insert a blank page if the page on which they occur is an odd-numbered one.

To get the same effect for an even-numbered page, change the '1' to '0'.
Reply With Quote
  #7  
Old 10-20-2016, 06:47 AM
Cov_ATC Cov_ATC is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2013
Novice
Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page
 
Join Date: Oct 2016
Posts: 5
Cov_ATC is on a distinguished road
Default

oh and with the Field Code encoded in a vba format, having the Page Blank text formatted?
Reply With Quote
  #8  
Old 10-20-2016, 01:31 PM
macropod's Avatar
macropod macropod is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You don't need a macro for this, either. All you need do is create the first field, with the required formatting, then cut it to the clipboard and use Find/Replace, where:
Find = ^b
Replace = ^c^&
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 10-24-2016, 03:43 AM
Cov_ATC Cov_ATC is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2013
Novice
Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page
 
Join Date: Oct 2016
Posts: 5
Cov_ATC is on a distinguished road
Default

Hi Macropod

I tried what you asked, manually adding a field, but it says
"Error! Unknown op code for conditional." when updating the field.

Im sorry to ask again, but I really need a code to add-in a field.
The reason being is that the Doc is constantly being reviewed and amended, with paragraphs and sections removed or added.
I need the editors to be able to, when adding a new Section, press a customised button on the Word ribbon, which will add a Section Break where the cursor is on the document and also automatically, just before the section break, add in the field code.
So they don't have to go over the whole document and do it themselves (as they have only basic Word knowledge)

Could you help me please?

I have tried this but with no avail,

Code:
Sub AddNextPageSectionBreakWithFieldCode()
 
   Selection.Fields.Add Selection.Range, wdFieldType.wdFieldIf, "{=MOD({PAGE},2)}= 1, {QUOTE 12} This page intentionally left blank", False
   
 With Selection
    'Adds a new line
    .TypeParagraph
   
    'puts a Next Page SectionBreak
    .InsertBreak Type:=wdSectionBreakNextPage
 End With
End Sub
Reply With Quote
  #10  
Old 10-27-2016, 11:47 PM
macropod's Avatar
macropod macropod is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 7 64bit Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Cov_ATC View Post
I tried what you asked, manually adding a field, but it says
"Error! Unknown op code for conditional." when updating the field.
All that means is that you've input the field code incorrectly.
Quote:
Originally Posted by Cov_ATC View Post
Im sorry to ask again, but I really need a code to add-in a field.
The reason being is that the Doc is constantly being reviewed and amended, with paragraphs and sections removed or added.
I need the editors to be able to, when adding a new Section, press a customised button on the Word ribbon, which will add a Section Break where the cursor is on the document and also automatically, just before the section break, add in the field code.
So they don't have to go over the whole document and do it themselves (as they have only basic Word knowledge)

Could you help me please?
The code you posted creates ordinary braces, not field braces, for both the outer field and the nested fields. For the reasons given in post #5, that won't work.

I'd suggest that, for macro purposes, you create a custom building block using a working copy of the field, then simply insert that where & when required. To see what would be required to turn the text representation in to a working field code, see Convert Text Representations of Fields to Working Fields in the Mailmerge Tips & Tricks 'Sticky' thread, at: https://www.msofficeforums.com/mail-...ps-tricks.html - and that's for a text string that's already in the document and selected.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 11-28-2021, 03:41 PM
Matthieu Matthieu is offline Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Windows 10 Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Office 2013
Novice
 
Join Date: Nov 2021
Posts: 1
Matthieu is on a distinguished road
Default

thx gmayor !
Reply With Quote
Reply

Tags
automatic, blank page, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2010: Section Break "Next" Creates Blank Page Eri Word 11 02-09-2017 03:36 PM
Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page start SOME sections on new page. drew345 Word 4 12-10-2015 08:34 PM
"Temporary" files that show up on the left of page livemusic Word 4 05-28-2015 04:10 PM
Word VBA - &quot;Page left Blank&quot; between Sections to make all Sections start on even page Mailing: how to make the "page number" in Word is the same as "row number" in excel w Jamal NUMAN Word 1 09-03-2011 11:37 AM
Pesky "Sections Breaks" in word doc Scarlet Word 0 02-09-2011 01:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:22 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft