?
Solved

How do I apply Personal.xls to existing workbooks?

Posted on 2009-04-07
6
Medium Priority
?
277 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

771 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