none
Condition field in footer causing document to not print correctly via ActiveDocument.Printout RRS feed

  • Question

  • We have the need to display a signoff block on each page of a document except for the last page.

    This has been achieved using the following field code:

    { IF { page } = { numpages }

    "{ DOCPROPERTY DocID }"

    "

    ....................................              ..................................

    Witness                                        Willmaker

    ....................................

    Witness

    { DOCPROPERTY DocID }" }

    Hopefully that gives you an idea - basically a lot of text in the footer for all pages but the last one, which will just show an ID. The documents we are printing a usually long - greater than 25 pages.

    If the user goes to File, Print, and then prints the document, it works. The problem is we have a macro that is used to print the document (it does things like setting the paper trays, setting the printer to single sided etc - but we have removed that functionality for this testing) So for now the print macro is just doing:

    ActiveDocument.Printout

    When printed out via the macro, the document will print the footers OK up until about page 22, and from there the footers will be blank for the rest of the document. The document also looks correct in Print Layout view, and the footers appear correct for all pages. Basically everything appears OK onscreen, it's only when the user picks up their print job they notice the issue.

    After doing a lot of research, we have found issues when using the numpages and sectionpages fields (refer to http://word.mvps.org/FAQs/AppErrors/PageXofY.htm) We have tried all of the suggestions on this page. Interestingly when we use the PAGEREF field with a bookmark at the end of the document, we get the same issue.

    I suspect the issue is Word is not paginating the document correctly because of the amount of text within the footer for all but the last page. This is causing the value of numpages to be incorrect when printing via the macro.

    Also, if you open the document and go to File, Print, the preview will show only 22 pages (even though there are 27), then you hit back, go in again and it shows 26, do it again and it then shows 27. As mentioned, printing from File Print works OK.

    I have tried updating the code to update all fields in footers, calling ActiveDocument.Paginate, even tried setting the view to Print Preview first (ActiveDocument.ActiveWindow.View = wdPrintPreview) as well as toggling the view several times, but nothing appears to make it print correctly apart from printing the document several times first, which is not exactly an environmentally friendly solution with such large documents.

    If you just print a range of the last few pages, you still get the problem. If I print a range of pages 25 - 27 the first time it will print only 2 pages, both with no footer, then next time via same code it will print 3 pages, one with the footer, and the other 2 blank.

    I've also tried ComputeStatistics to see if it helps with the numpages field value.

    Below is some of the code I've tried (in various combinations) to try work around the problem - nothing has worked. The Printout line has been changed to prevent background printing (as per the suggestion above) and to print just the last 3 pages of the test document, but the same problem occurs when specifying no parameters.

    It's not great code - just a bunch of quick and dirty attempts to try find a solution. Try creating a document with the above field in the primary footer, and make the document 27 pages long with some dummy text. Then try get it to print correctly!

    Sub TestPrint()
    'Application.ScreenUpdating = False

    ' Make sure the bookmarks is at the end of the document
    If ActiveDocument.Bookmarks.Exists("myEndOfDoc") Then
        If ActiveDocument.Bookmarks("myEndOfDoc").Range.End <> ActiveDocument.Bookmarks("\EndOfDoc").Range.End Then
            ActiveDocument.Bookmarks("myEndOfDoc").Delete
            ActiveDocument.Bookmarks.Add "myEndOfDoc", ActiveDocument.Bookmarks("\EndOfDoc").Range
        End If
    End If

    ' Move the selection to the end of the document
    Dim p As Range
    Set p = Selection.Range
    ActiveDocument.Characters.Last.Select
    Selection.Collapse wdCollapseEnd

    ' Try updating all fields in the footer
    Dim i As Integer
    For i = 1 To ActiveDocument.Sections.Count
        ActiveDocument.Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields.Update
        ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields.Update
        ActiveDocument.Sections(i).Footers(wdHeaderFooterEvenPages).Range.Fields.Update
    Next

    ' Try repaginating the document
    ActiveDocument.Repaginate

    ' Try changing the view
    Dim currentview As Long
    currentview = ActiveDocument.ActiveWindow.View
    ActiveDocument.ActiveWindow.View = wdPrintPreview
    Application.ScreenRefresh
    ActiveDocument.ActiveWindow.View = wdNormalView
    ActiveDocument.ActiveWindow.View = wdPrintPreview
    Application.ScreenRefresh

    ' Pause for 2 secs
    TimerStart = Timer
    Do While Timer < TimerStart + 2
    Loop

    ActiveDocument.ActiveWindow.View = wdNormalView
    ActiveDocument.Characters.Last.Select
    Selection.Collapse wdCollapseEnd
    ActiveDocument.ActiveWindow.View = wdPrintPreview
    ActiveDocument.Characters.Last.Select
    Selection.Collapse wdCollapseEnd
    Application.ScreenRefresh

    ' Pause for 2 secs
    TimerStart = Timer
    Do While Timer < TimerStart + 2
    Loop

    ActiveDocument.Range.ComputeStatistics (wdStatisticWords)

    ActiveDocument.PrintOut False, , wdPrintFromTo, , "25", "27"
    ActiveDocument.ActiveWindow.View = currentview
    p.Select
    Application.ScreenUpdating = True
    End Sub

    The problem was reported in Word 2010, but I can replicate (and have been testing) with Word 2013.

    Any tips, pointers or workarounds would be greatly appreciated - even if there is another way to achieve a different footer on only the last page (without having to create another section is the catch - desperately trying to avoid using multiple sections due to usability)

    Thanks in advance!

    Wednesday, February 5, 2014 5:30 AM

Answers

  • Hi all,

    Believe it or not - I found the answer. Cindy's suggestion of using a drawing rectangle got me thinking about the problem a different way (so thank's Cindy!)

    The problem is really easy to fix - set the bottom margin for the pages to be big enough to fit the text. The bottom margin was left as the default (2.54cm) When I change this to be bigger (i.e. 5.5cm to fit the bigger footer) the document paginates correctly, and therefore prints correctly every time no matter how you do it.

    The only problem is the last page may not have enough space on it for what I need sometimes, but I can live with that.

    I can't believe it was solved with something so simple in the end. Can't even begin to tell you how much time (and paper) I've wasted in trying to nut this one out.

    Cheers

    Chris

    • Marked as answer by Sir HC Friday, February 7, 2014 5:28 AM
    Friday, February 7, 2014 5:28 AM

All replies

  • Hi Sir HC Mosmar

    Printing isn't my forté, but one thing I have learned over the years: using the parameter to control background printing isn't reliable. Test using

    ActiveDocument.Options.PrintBackground = False

    The other idea that occurs to me is to trigger Word's built-in command, rather than use the object model command. That might get Word to do the same thing as when the user prints (using the quick print button):

    Application.CommandBars.ExecuteMso("FilePrintQuick")


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, February 5, 2014 9:49 AM
    Moderator
  • It's possible your Office installation has a fault. Just to be on the safe side, you could try repairing the Office installation (via Programs & Features > Microsoft Office > Change in the Windows Control Panel).

    It's also possible your document has acquired some form of corruption. Corrupt documents can often be 'repaired' by inserting a new, empty, paragraph at the very end, copying everything except that new paragraph to a new document based on the same template, closing the old document and saving the new one over it.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, February 5, 2014 10:07 AM
  • Hi Paul,

    It's not an Office installation issue or document corruption issue as I can replicate it on any machine with any document. I have replicated it in Word 2010 and Word 2013 on different machines (plus my client is getting it occurring for all documents with a footer like this on about 300 different machines) and have created a new document from scratch based on Normal.dotm.

    I suspect it's just Word struggling with how big the document really is with such a large conditional footer.

    Cheers

    Chris

    Wednesday, February 5, 2014 11:08 PM
  • Hi Cindy,

    Thanks for your suggestions, but neither of them worked. I really thought you were on to something with the "FilePrintQuick" option.

    Regards

    Chris

    Thursday, February 6, 2014 12:00 AM
  • Hi Chris

    Too bad FileQuckPrint didn't work..

    The only other thing that occurs to me at the moment would be to try SendKeys (possibly with some DoEvent or Loop stuff), in order to more closely mimic the user. Not something I usually recommend, but as a last resort...

    Ah, and an idea that came to me last night: Don't put all of this in an IF field code. Instead, place a drawing rectangle over the content of the last page.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, February 6, 2014 4:28 PM
    Moderator
  • Hi Cindy,

    Thanks yet again. I'm always reluctant using SendKeys - but I will try it as an absolute last resort (which I'm rapidly getting too)

    With the drawing rectangle, is there a way to always anchor it to the last page? The problem will be if the users add some more pages at the end of the document the rectangle will get left behind. I know I could check for this on print and move it if necessary, it would be nice to avoid it.

    I'll keep playing around and let you know how I go. I'm still holding out hope for using print preview somehow...

    Regards

    Chris

    Friday, February 7, 2014 4:44 AM
  • Hi all,

    Believe it or not - I found the answer. Cindy's suggestion of using a drawing rectangle got me thinking about the problem a different way (so thank's Cindy!)

    The problem is really easy to fix - set the bottom margin for the pages to be big enough to fit the text. The bottom margin was left as the default (2.54cm) When I change this to be bigger (i.e. 5.5cm to fit the bigger footer) the document paginates correctly, and therefore prints correctly every time no matter how you do it.

    The only problem is the last page may not have enough space on it for what I need sometimes, but I can live with that.

    I can't believe it was solved with something so simple in the end. Can't even begin to tell you how much time (and paper) I've wasted in trying to nut this one out.

    Cheers

    Chris

    • Marked as answer by Sir HC Friday, February 7, 2014 5:28 AM
    Friday, February 7, 2014 5:28 AM
  • Hi Chris

    <<With the drawing rectangle, is there a way to always anchor it to the last page? The problem will be if the users add some more pages at the end of the document the rectangle will get left behind. I know I could check for this on print and move it if necessary, it would be nice to avoid it.>>

    For the sake of completeness in case others run into issues that might profit from the suggestions made in this discussion:

    No, there's no way to anchor to the last page. You could use a macro, however, that triggers on DocumentBeforePrint to insert/position the rectangle on the last page.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, February 7, 2014 8:19 AM
    Moderator
  • Hi Chris

    <<The problem is really easy to fix - set the bottom margin for the pages to be big enough to fit the text. The bottom margin was left as the default (2.54cm) When I change this to be bigger (i.e. 5.5cm to fit the bigger footer) the document paginates correctly, and therefore prints correctly every time no matter how you do it.>>

    That's very interesting, indeed. Thanks for doing such thorough research and taking a moment to share this with us :-)

    Now that you've discovered it, the entire problem scenario makes sense: Word's layout engine is "hanging up" on processing these calculations when sending the job to the printer. Why it should manage when the user does it, vs. the object model, is still a mystery, of course...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, February 7, 2014 8:21 AM
    Moderator