Bookmark that moves!

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
 
C

Chuck Henrich

When you bookmark AUTO_BLANK_LAST_PAGE using the last paragraph range, it
also takes the page break before the paragraph. Instead, how about using the
rEnd range to set the bookmark, first collapsing rEnd to its end eg:
'Bookmark end of document, not last paragraph
rEnd.Collapse wdCollapseEnd
ActiveDocument.Bookmarks.Add Name:="AUTO_BLANK_LAST_PAGE",
Range:=rEnd

Revised code below:

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
'Bookmark end of document, not last paragraph
rEnd.Collapse wdCollapseEnd
ActiveDocument.Bookmarks.Add Name:="AUTO_BLANK_LAST_PAGE",
Range:=rEnd
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

HTH
 
N

Nigel Hoult

Thanks for the suggestion, Chuck. Unfortunately, including the page break before the paragraph in
the bookmark was deliberate; that way, when you run the macro again, it first removes any
"intentionally blank" page, then re-inserts it if required. Without that, if you added a page to a
document with an odd number of pages, and ran the macro again, you would end up with two blank pages
at the end.

It almost seems to me that the problem is concerned with nesting bookmarks. The LASTPAGE bookmark
(which spans no characters) is perhaps inside the AUTO_BLANK_LAST_PAGE bookmark, rather than after
the end of it, and maybe (for some unknown reason) updating the fields moves it to before the
latter?

Nigel Hoult
 
C

Chuck Henrich

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!

I've tried to recreate your problem on a clean document using your code but
Ctrl+A, F9 doesn't move any bookmarks for me - LASTPAGE stays on the last
page. The only bookmark I see on the penultimate page is
AUTO_BLANK_LAST_PAGE (which starts there). Perhaps there's something wrong
with your field definition - maybe it's referring to AUTO_BLANK_LAST_PAGE
instead of LASTPAGE. Otherwise beats me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA Export to PDF 0
Deleting hard returns 2
bookmark sections 0
inserted page referencing a bookmark 2
Bookmark Menu 5
if bookmark.Exist (Left("nut", 3) 2
Date Field Clear 1
Finding Hidden text in word VBA 2

Top