C
Clif McIrvin
xl2010
I'm attempting to create a macro to install a set of headers and footers
that I only ocassionally use. Long ago I created a .xlt; but I find that
in most cases I want to add the headers / footers to an existing sheet
so the template isn't really being very helpful.
Working from the template, I used the macro recorder to create some
initial code, which I have slightly customized. What I am finding is
that if I disable all the
Application.PrintCommunication = False
statements that the recorder put in the macro does what I want; but if I
leave them in place the results are unpredictable.
Evidently either a) I'm too impatient and not waiting for all the cached
commands to execute, b) commands are getting lost and/or mangled in the
cache or c) something else <grin>.
If someone has a solution that'd be great ... if not, at least the code
does what I want, albeit slowly (still faster than manually!!) if I
leave PrintCommunication on.
Here's the code I'm working with:
Sub AddDefaultHeaders()
If ActiveSheet Is Nothing Then
Exit Sub
End If
ActiveSheet.PageSetup.LeftHeaderPicture.FileName = _
"C:\Documents and Settings\username\My Documents\" _
& "My Pictures\logo.png"
With ActiveSheet.PageSetup.LeftHeaderPicture
.Height = 69
.Width = 82.5
End With
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.6)
.RightMargin = Application.InchesToPoints(0.6)
.TopMargin = Application.InchesToPoints(1.11)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.31)
.FooterMargin = Application.InchesToPoints(0.2)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
.LeftHeader = "&G"
'Application.Wait 1000
'DoEvents
.CenterHeader = _
"&""Century Schoolbook,Bold""&16My Company Name "
.RightHeader = "&""Century Schoolbook,Bold""&9Quality" _
& Chr(10) & "Assurance"
.LeftFooter = "&9&Z" & Chr(10) & "&F"
.CenterFooter = ""
.RightFooter = "&9Printed &D &T"
End With
Application.PrintCommunication = True
ActiveSheet.PrintPreview EnableChanges:=True
End Sub
I'm attempting to create a macro to install a set of headers and footers
that I only ocassionally use. Long ago I created a .xlt; but I find that
in most cases I want to add the headers / footers to an existing sheet
so the template isn't really being very helpful.
Working from the template, I used the macro recorder to create some
initial code, which I have slightly customized. What I am finding is
that if I disable all the
Application.PrintCommunication = False
statements that the recorder put in the macro does what I want; but if I
leave them in place the results are unpredictable.
Evidently either a) I'm too impatient and not waiting for all the cached
commands to execute, b) commands are getting lost and/or mangled in the
cache or c) something else <grin>.
If someone has a solution that'd be great ... if not, at least the code
does what I want, albeit slowly (still faster than manually!!) if I
leave PrintCommunication on.
Here's the code I'm working with:
Sub AddDefaultHeaders()
If ActiveSheet Is Nothing Then
Exit Sub
End If
ActiveSheet.PageSetup.LeftHeaderPicture.FileName = _
"C:\Documents and Settings\username\My Documents\" _
& "My Pictures\logo.png"
With ActiveSheet.PageSetup.LeftHeaderPicture
.Height = 69
.Width = 82.5
End With
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.6)
.RightMargin = Application.InchesToPoints(0.6)
.TopMargin = Application.InchesToPoints(1.11)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.31)
.FooterMargin = Application.InchesToPoints(0.2)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
.LeftHeader = "&G"
'Application.Wait 1000
'DoEvents
.CenterHeader = _
"&""Century Schoolbook,Bold""&16My Company Name "
.RightHeader = "&""Century Schoolbook,Bold""&9Quality" _
& Chr(10) & "Assurance"
.LeftFooter = "&9&Z" & Chr(10) & "&F"
.CenterFooter = ""
.RightFooter = "&9Printed &D &T"
End With
Application.PrintCommunication = True
ActiveSheet.PrintPreview EnableChanges:=True
End Sub