View RSS Feed

It looks like you are trying to write a blog...

Excel 2010 - Printer Fails to Communicate

Rate this Entry
by , 5th May 2011 at 02:52 PM (1498 Views)
Long term users of Excel VBA will be familiar with slow processing of page set up if code like the following is used:

With ActiveWorksheet.PageSetup
.CenterHeader = "Title of the Worksheet"
.LeftFooter = "Today's Date"
.RightFooter = "Page &P of &N"
.PrintArea = "$A$1:$H$28"
End With


The problem with the code is that, despite the use of the With statement, each line requires a call to the printer driver, rather than the whole statement being sent once. The most common solution is to fall back on the Page.SetUp Excel 4 macro. An example of the kind of code that a programmer might use can be found here: McGimpsey & Associates : Excel : Bypassing Slow VBA PageSetup.

Changes to Excel 2010 were intended to remove the need to fall back on legacy code by switching on and off the printer communication (additions to code in blue):

Application.PrintCommunication = False

With ActiveWorksheeet.PageSetup
.CenterHeader = "Title of the Worksheet"
.LeftFooter = "Today's Date"
.RightFooter = "Page &P of &N"
.PrintArea = "$A$1:$H$28"
End With


Application.PrintCommunication = True

Further information can be found here: Microsoft Excel - Migrating Excel 4 Macros to VBA.

Unfortunately, it doesn't work as intended! Although I present the problem in terms of the footer, the same applies to the header.

The footer has three components: Left, Center and Right. If these have not yet been set the macro works as intended. However, if any of these have been previously assigned to, only the last footer section listed in the macro will have its text changed. That is, whichever is the last of LeftFooter, CenterFooter and RightFooter as listed in code order is changed and the others are left untouched.

Additionally, using the familiar "Page &P of &N" gets cut off if more than one section of the footer is assigned to.

It appears others have experienced the same problem:



In each case the advice is not to use PrintCommunication!

This is a real pity as apart from problems with (re-) setting the footers and headers, it works exactly as intended. Nonetheless, I am still using Excel 4 macros in version 14 >.<

Updated 19th October 2011 at 12:30 PM by Pico

Categories
Office Agonistes

Comments

  1. kobusb's Avatar
    I experienced the same problem. Interestingly it does not occur if I slowly step through the code. If I quickly step through the code then it does not properly write the footers or headers.
  2. Pico's Avatar
    Quote Originally Posted by kobusb
    I experienced the same problem. Interestingly it does not occur if I slowly step through the code. If I quickly step through the code then it does not properly write the footers or headers.
    Intriguing! Thanks for sharing this.

    It makes me wonder whether there is a way to mimic this effect in the code. (My first thought was to try something like DoEvents, but that doesn't work.)
  3. kobusb's Avatar
    I set "Application.PrintCommunication = True" and that seems to solve the problem but I have not tested it extensively i.e. turning off print communication seems to create the problem.

Trackbacks

Total Trackbacks 0
Trackback URL: