N
Nigel Hoult
I use a Word template that has a bookmark called LASTPAGE right at the end (so that the footers can
display "Page M of N", where "N" is the last page number in the current section rather than in the
document. (Pages are numbered i to x, 1 to 20 for example.)
Recently, I wrote the VBA macro below to insert an "intentionally blank" page for double-sided
printing if the last page number was odd. This works fine, except that if, after running it (and
having a blank page added), you update the fields by doing ctrl+A, F9, the "LASTPAGE" bookmark moves
back to the penultimate page, so messing up the numbering! This is using Word 2003.
I'd be grateful if anyone could explain this, or suggest a workround.
Regards,
Nigel Hoult
Sub AddBlankPages()
' Macro to add an "intentionally blank" page at the end of the last section where that
' section ends on an odd page
' Added text is identified by bookmark "AUTO_BLANK_LAST_PAGE", which will be removed before
' processing so that the macro can be used repeatedly on the same document.
' See if there's a bookmark called LASTPAGE: if so, remove it
' It will be re-inserted after all changes are made
hasLastPage = False
If ActiveDocument.Bookmarks.Exists("LASTPAGE") = True Then
ActiveDocument.Bookmarks("LASTPAGE").Delete
hasLastPage = True
End If
' Remove any previously added text
If ActiveDocument.Bookmarks.Exists("AUTO_BLANK_LAST_PAGE") = True Then
ActiveDocument.Bookmarks("AUTO_BLANK_LAST_PAGE").Range.Delete
Set rEnd = ActiveDocument.Range
rEnd.Collapse (wdCollapseEnd)
rEnd.Delete Unit:=wdCharacter, Count:=1
End If
' Scan through the sections to find if any pages need adding
For Each Section In ActiveDocument.Sections
' Get the end of the section
Set rEnd = Section.Range
rEnd.Collapse (wdCollapseEnd)
If (Section.Index = ActiveDocument.Sections.Count) Then
' If it is the last section
endPage = rEnd.Information(wdActiveEndAdjustedPageNumber)
If (endPage Mod 2 = 1) Then
rEnd.InsertBreak (wdPageBreak)
rEnd.InsertAfter ("This page intentionally blank")
Set r = Section.Range
Set lastPara = r.Paragraphs(r.Paragraphs.Count)
halfWay = (Section.PageSetup.PageHeight - Section.PageSetup.TopMargin) / 2
lastPara.Style = wdStyleNormal
lastPara.SpaceBefore = halfWay
lastPara.Alignment = wdAlignParagraphCenter
ActiveDocument.Bookmarks.Add Name:="AUTO_BLANK_LAST_PAGE", Range:=lastPara.Range
End If
End If
Next
' Finally, replace LASTPAGE bookmark if it was there originally
If hasLastPage = True Then
Set rEnd = ActiveDocument.Range
rEnd.Collapse (wdCollapseEnd)
ActiveDocument.Bookmarks.Add Name:="LASTPAGE", Range:=rEnd
End If
End Sub
display "Page M of N", where "N" is the last page number in the current section rather than in the
document. (Pages are numbered i to x, 1 to 20 for example.)
Recently, I wrote the VBA macro below to insert an "intentionally blank" page for double-sided
printing if the last page number was odd. This works fine, except that if, after running it (and
having a blank page added), you update the fields by doing ctrl+A, F9, the "LASTPAGE" bookmark moves
back to the penultimate page, so messing up the numbering! This is using Word 2003.
I'd be grateful if anyone could explain this, or suggest a workround.
Regards,
Nigel Hoult
Sub AddBlankPages()
' Macro to add an "intentionally blank" page at the end of the last section where that
' section ends on an odd page
' Added text is identified by bookmark "AUTO_BLANK_LAST_PAGE", which will be removed before
' processing so that the macro can be used repeatedly on the same document.
' See if there's a bookmark called LASTPAGE: if so, remove it
' It will be re-inserted after all changes are made
hasLastPage = False
If ActiveDocument.Bookmarks.Exists("LASTPAGE") = True Then
ActiveDocument.Bookmarks("LASTPAGE").Delete
hasLastPage = True
End If
' Remove any previously added text
If ActiveDocument.Bookmarks.Exists("AUTO_BLANK_LAST_PAGE") = True Then
ActiveDocument.Bookmarks("AUTO_BLANK_LAST_PAGE").Range.Delete
Set rEnd = ActiveDocument.Range
rEnd.Collapse (wdCollapseEnd)
rEnd.Delete Unit:=wdCharacter, Count:=1
End If
' Scan through the sections to find if any pages need adding
For Each Section In ActiveDocument.Sections
' Get the end of the section
Set rEnd = Section.Range
rEnd.Collapse (wdCollapseEnd)
If (Section.Index = ActiveDocument.Sections.Count) Then
' If it is the last section
endPage = rEnd.Information(wdActiveEndAdjustedPageNumber)
If (endPage Mod 2 = 1) Then
rEnd.InsertBreak (wdPageBreak)
rEnd.InsertAfter ("This page intentionally blank")
Set r = Section.Range
Set lastPara = r.Paragraphs(r.Paragraphs.Count)
halfWay = (Section.PageSetup.PageHeight - Section.PageSetup.TopMargin) / 2
lastPara.Style = wdStyleNormal
lastPara.SpaceBefore = halfWay
lastPara.Alignment = wdAlignParagraphCenter
ActiveDocument.Bookmarks.Add Name:="AUTO_BLANK_LAST_PAGE", Range:=lastPara.Range
End If
End If
Next
' Finally, replace LASTPAGE bookmark if it was there originally
If hasLastPage = True Then
Set rEnd = ActiveDocument.Range
rEnd.Collapse (wdCollapseEnd)
ActiveDocument.Bookmarks.Add Name:="LASTPAGE", Range:=rEnd
End If
End Sub