Solved

How do I apply Personal.xls to existing workbooks?

Posted on 2009-04-07
6
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

697 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