C
Christopher B via OfficeKB.com
I wrote a macro that adds a section break with a blank page before it (blank
other than saying "Blank Page" in the middle). This page appears only if it
is an even-numbered page. The macro works fine, but it's made from recording
within Word. I've tried to understand how to do this with better VBA but
can't find a good explanation of creating and manipulating fields with VBA.
(O'Reilly's "Word Macros" gives a glimpse but not enough to go on.) I'll put
my code here. What I'm hoping is that someone can show how good VBA would do
this. (I learned about this field from John McGhie's page at
http://word.mvps.org/FAQs/TblsFldsFms/InsEvnPgEndChap.htm.)
If you try my macro as written, your document or template needs paragraph
styles called "Body Text" and "BlankPage" (mine is centered, italic, 250pt
before). When your document has the conditional field in it, it must be
updated to work; this usually happens automatically at appropriate times.
Public AddSectBrkAndCondPage()
'
' Adds a section break and a conditional blank page saying
' "Blank page", which appears only if the preceding section
' ended on an odd-numbered page.
' Macro recorded 9/26/2005 by Christopher Brewster
'
' Conditional field will be in the following form. Spaces
' are important, and {} characters are not from those keys
' on the keyboard, but are generated by Ctrl-F9.
' { IF { =MOD( { PAGE },2) } = 0 " " "
' ----------------Page Break--------------
' Para
' Blank page" }
' ======Section Break (Next Page)=========
' Add section break
' Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveLeft Unit:=wdCharacter, Count:=1
' Create three nested fields
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
' Add var. to be tested within the innermost field
Selection.TypeText Text:="PAGE"
' Go to beginning of the line and add code details
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:="IF "
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:="=MOD( "
Selection.MoveRight Unit:=wdCharacter, Count:=8
Selection.TypeText Text:=",2) "
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" = 0 "" "" """
' Paragraph mark must appear on blank page, not on last page
' of the section...
' Add page break, which also creates a paragraph mark
Selection.InsertBreak Type:=wdPageBreak
' Go back and delete paragraph mark
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.Delete Unit:=wdCharacter, Count:=1
' Go forward and add paragraph mark
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
' Add label to display on the blank page
Selection.TypeText Text:="Blank page"""
' Go back and set correct paragraph styles
' The "Blank page" line has no para mark. The BlankPage
' style applies to the label and the section break
Selection.Style = ActiveDocument.Styles("BlankPage")
' Set the para mark and page break as Body Text
Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Style = ActiveDocument.Styles("Body Text")
' Put the field into effect
Selection.Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End sub
other than saying "Blank Page" in the middle). This page appears only if it
is an even-numbered page. The macro works fine, but it's made from recording
within Word. I've tried to understand how to do this with better VBA but
can't find a good explanation of creating and manipulating fields with VBA.
(O'Reilly's "Word Macros" gives a glimpse but not enough to go on.) I'll put
my code here. What I'm hoping is that someone can show how good VBA would do
this. (I learned about this field from John McGhie's page at
http://word.mvps.org/FAQs/TblsFldsFms/InsEvnPgEndChap.htm.)
If you try my macro as written, your document or template needs paragraph
styles called "Body Text" and "BlankPage" (mine is centered, italic, 250pt
before). When your document has the conditional field in it, it must be
updated to work; this usually happens automatically at appropriate times.
Public AddSectBrkAndCondPage()
'
' Adds a section break and a conditional blank page saying
' "Blank page", which appears only if the preceding section
' ended on an odd-numbered page.
' Macro recorded 9/26/2005 by Christopher Brewster
'
' Conditional field will be in the following form. Spaces
' are important, and {} characters are not from those keys
' on the keyboard, but are generated by Ctrl-F9.
' { IF { =MOD( { PAGE },2) } = 0 " " "
' ----------------Page Break--------------
' Para
' Blank page" }
' ======Section Break (Next Page)=========
' Add section break
' Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveLeft Unit:=wdCharacter, Count:=1
' Create three nested fields
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
' Add var. to be tested within the innermost field
Selection.TypeText Text:="PAGE"
' Go to beginning of the line and add code details
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:="IF "
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:="=MOD( "
Selection.MoveRight Unit:=wdCharacter, Count:=8
Selection.TypeText Text:=",2) "
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" = 0 "" "" """
' Paragraph mark must appear on blank page, not on last page
' of the section...
' Add page break, which also creates a paragraph mark
Selection.InsertBreak Type:=wdPageBreak
' Go back and delete paragraph mark
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.Delete Unit:=wdCharacter, Count:=1
' Go forward and add paragraph mark
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
' Add label to display on the blank page
Selection.TypeText Text:="Blank page"""
' Go back and set correct paragraph styles
' The "Blank page" line has no para mark. The BlankPage
' style applies to the label and the section break
Selection.Style = ActiveDocument.Styles("BlankPage")
' Set the para mark and page break as Body Text
Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Style = ActiveDocument.Styles("Body Text")
' Put the field into effect
Selection.Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End sub