Solved

How do I apply Personal.xls to existing workbooks?

Posted on 2009-04-07
6
272 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all theā€¦
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

840 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