Solved

How do I apply Personal.xls to existing workbooks?

Posted on 2009-04-07
6
268 Views
Last Modified: 2012-05-06
I need to know how to write a Before_Print macro that will apply to all existing workbooks on my machine when I open them.

I have a folder of over 100 excel files that I need to open, print entire workbook, and close. I have stored the macro to select all sheets in the Before_Print Sub in Personal.xls, but when I open other workbooks, all the sheets are not selected when I print.

Thanks!
0
Comment
Question by:jamisonFox
  • 3
  • 3
6 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 24088164
Hello jamisonFox,

To do this, you need to instantiate a class to represent the Application object; that exposes the Application's
WorkbookBeforePrint event.

In Personal.xls, add a class module with the following code:

Option Explicit

Public WithEvents App As Application

Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, Cancel As Boolean)
   
    MsgBox "Foo!"
   
End Sub

and name that class module MyApplicationClass.

Now, in the ThisWorkbook module, add this code:

Option Explicit

Dim X As New MyApplicationClass

Private Sub Workbook_Open()
   
    Set X.App = Application
   
End Sub

Since you did not mention what your code needs to do, all it does now is show that silly MsgBox when you go
to print, or you do a print preview.

Regards,

Patrick
0
 

Author Comment

by:jamisonFox
ID: 24088352
I get a compile error: "Only valid in object module" for the line:

Public WithEvents App As Application

Any ideas?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24088413
jamisonFox said:
>>Any ideas?

Yes, it appears that you put that code in a regular module, and not in a class module as I suggested :)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:jamisonFox
ID: 24088652
Excellent!

Foo! pops up whenever I select print on other workbooks.

Now, what is the best practice to select all active worksheets so that they are printed?

Or, if possible, is there a way to change the default print selection to 'entire workbook' instead of active sheet?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24088760
jamisonFox,

Not sure is this will work, but you could try the following for the event code:

Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, Cancel As Boolean)
   
    Cancel = True
    Wb.PrintOut
   
End Sub

The big danger that I foresee here is that the BeforePrint gets triggered when you go to print preview, and you
might not want the code to run like this on a print preview...

Regards,

Patrick
0
 

Author Comment

by:jamisonFox
ID: 24089018
Thanks for the help.

I used the code below to select all visible sheets, which in turn printed them all.
Option Explicit
 

Public WithEvents App As Application
 

Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, Cancel As Boolean)
 

   Dim ws As Worksheet
 

   For Each ws In Sheets

      If ws.Visible Then ws.Select (False)

   Next
 

End Sub

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now