Differentiate between 'Print' & 'Print Preview' in VBA c

Mattantaliss

Board Regular
Joined
Sep 6, 2005
Messages
52
Hello,

I have set up some code in a Workbook_BeforePrint Sub to log information as worksheets are printed. That is, for example, I have sheetA, sheetB, and sheetLog, and whenever a user prints either sheetA or sheetB, my code will take some information (e.g., name and date) from that sheet and copy it to sheetLog.

The issue I have now discovered is that the information will also be logged if the user merely executes a Print Preview, which makes sense to me. I am wondering, though, if there is a way to differentiate, in the code, between an actual 'Print' and just a 'Print Preview'. I am envisioning something like:
Code:
   If <just Print Preview> Then
      <don't log information>
   Else 'actually Printing
      <do stuff as normal>
   End If
Does such a distinction between 'Print' and 'Print Preview' exist?
 
rafaaj2000 :)

For me it was clear you could solve this !!
For me it's working.
Next, just save the WorkBook , close it and reopen it again.
For your information:
did you know you can run the Workbook_Open procedure: you don't need to close and open again

best regards,
Erik

Thanks for the feedback Erik.

And Yes,as you said, you could easily run the Workbook_Open without restarting the wb.

Regards.
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi rafaaj2000,

Thank you so much for that code. It does look to be working just like I was envisioning, with one small exception:
If I open the workbook and print the worksheet, it fails to execute the code on the first time. It seems as though something is not getting set properly on the initialization, but after the first execution of print (or print preview), all works just fine.

I'm attempting to track down exactly what's happening with that first time not executing correctly, but if you notice the fix quickly and can get back to me, I would greatly appreciate it.

Again, thanks for it all.

matt
 
Upvote 0
...and of course, right after I posted that, I tracked it down :)

In the workbook's Enable_PrintPrevClass() sub, objPrintPrv.SkipBeforePrintEvent needs to be changed from '= True' to '= False'. So the corrected sub would be:
Code:
Sub Enable_PrintPrevClass()

    If objPrintPrv Is Nothing Then
        Set objPrintPrv = New PrintPrevClass
        objPrintPrv.SkipBeforePrintEvent = False
        objPrintPrv.execute
    End If
   
End Sub

Thank you [again] rafaaj2000...

matt
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,884
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top