BeforePrint Event

  • Thread starter Thread starter Woody
  • Start date Start date
W

Woody

HI,

I am trying to develop some VBA code in excel 2003 that will
automatically place a specific header and footer on any sheet I print
from excel.

So far I have created the following code in my personal workbook.

Public Sub App_WorkbookBeforePrint(Cancel As Boolean)
ActiveSheet.PageSetup.RightFooter = "&D &T"
ActiveSheet.PageSetup.CenterFooter = "&F"
End Sub

It appears to work for that workbook but not others I have open.

Can anyone suggest how I can make this code apply to the excel
application instead of to a particular workbook?

Thanks,
 
Woody,

You need application events, and you need to invoke them from a workbook
that is opened whenever Excel is started. An obvious place is Personal.xls.
Add this code

Public WithEvents App As Application

Private Sub Workbook_Open()
Set App = Application
End Sub

Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, Cancel As Boolean)
With Wb.ActiveSheet.PageSetup
.RightFooter = "&D &T"
.CenterFooter = "&F"
End With
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code
 

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

Back
Top