Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I apply Personal.xls to existing workbooks?

Posted on 2009-04-07
6
Medium Priority
?
280 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 93

Accepted Solution

by:
Patrick Matthews earned 1000 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 93

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 93

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

636 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