MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2002, 07:05 AM   #1
JohnG
Board Regular
 
Join Date: Feb 2002
Location: SRC
Posts: 165
Default

am passing this on because I was asked this and do not know an answer.

The reason is that both the Print button and the Print Preview button activate the Before_Print event that has been set up and what needs to take place is that it should only be run if the Print button was selected.
NOTE: Print Preview actualy calls the event twice, 1st when it is pressed and 2nd if the print option within the print previeew is selected.
JohnG is offline   Reply With Quote
Old Feb 22nd, 2002, 08:59 AM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

I'm certain this was answered on the old messageboard because I was looking to solve that but couldn't. I've had a look at the archives and can't find it. Maybe I dreamed it, but it was very smart non-intuitive solution.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Feb 22nd, 2002, 09:50 AM   #3
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Ok,

I don't think there is a foolproof way to capture all different ways of choosing print/print preview but this should get you off to a good start.

In the VB Editor insert a class module and use this code:-

Option Explicit

Private WithEvents btnPrint As Office.CommandBarButton
Private WithEvents btnPrintPreview As Office.CommandBarButton

Private Sub btnPrint_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
'Print was pressed
CancelDefault = True
MsgBox "Print was pressed but I ain't printing!"
'Do whatever you feel need
End Sub

Private Sub btnPrintPreview_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
'Print preview was pressed
CancelDefault = True
MsgBox "How rude, you clicked Print Preview"
'Now do whatever you want
End Sub

Public Sub SetPrintPreview(btn As Office.CommandBarButton)
Set btnPrintPreview = btn
End Sub

Public Sub SetPrint(btn As Office.CommandBarButton)
Set btnPrint = btn
End Sub

Then in a normal module use this procedure:-

Option Explicit

Public cBar As New Class1

Sub Initiate()
Dim combar As CommandBar
Dim compop As CommandBarPopup
Dim combut As CommandBarButton

'First capture Print preview button being pressed
Set combar = Application.CommandBars("Worksheet Menu Bar")
Set compop = combar.Controls("File")
Set combut = compop.Controls(11)
cBar.SetPrintPreview combut

'Now set up print button being pressed
Set combut = compop.Controls(12)
cBar.SetPrint combut

End Sub


I've done a quick check and it captures the Print option from the File menu, Print preview from the file menu and the print preview button from the Standard toolbar. You should be able to modify the code to include the Print button from the standard toolbar (I've no idea why this isn't captured by the code as it is).

The user will still be able to do things like File, Page Setup, Print Preview. I guess you could disable the Page setup button.

Let me know how you get on,

Regards,
D
dk is offline   Reply With Quote
Old Feb 22nd, 2002, 09:58 AM   #4
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Forgot to mention.

You'll need to run the procedure Initiate before anything will work.

Regards,
D
dk is offline   Reply With Quote
Old Feb 22nd, 2002, 10:25 AM   #5
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Juan Pablo posted this in reply to a similar question from me on the old board, but this was for a custom toolbar. It may be of some help but Juan Pablo I think's your man for this. This is a copy of his answer: -

I've recently accomplished this using

Application.Caller

Try this example.

Option Explicit

Sub MakeBar()
Dim bar As CommandBar
Dim bt As CommandBarButton
Dim i As Integer

On Error Resume Next
Application.CommandBars("MyBar").Delete
On Error GoTo 0

Set bar = Application.CommandBars.Add("MyBar", msoBarFloating, False, True)
With bar
For i = 1 To 3
Set bt = .Controls.Add(msoControlButton)
With bt
.Caption = "# " & i
.Style = msoButtonCaption
.OnAction = "Test"
End With
Next i
.Visible = True
End With
End Sub

Private Sub Test()
MsgBox CommandBars("MyBar").Controls(Application.Caller(1)).Caption
End Sub

First run "MakeBar" and click any of the buttons. The caption of the button will appear. Hope this gets you there.

By the way, Application.Caller is another way of making sure a macro IS NOT run from Alt - F8, check the help to see what i mean.

Juan Pablo G.


: I've set up a sheet to run a summary on a couple of other (quarterly) sheets. My code below adds the custom menu items to the command bar. I was wondering, rather than having a load of sub's for each quarter, whether there's a way of checking which of the menu items was selected. That way I could just have one sub and could assign variables depending on which quarter and year was chosen.

Mudface is offline   Reply With Quote
Old Feb 22nd, 2002, 03:14 PM   #6
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

As far as I can see that doesn't satisfy any of the requirements of the original question. The reply I posted allows for capture of the user pressing print/print preview as best you can with VBA. The reply above is for a custom toolbar - not what the question was related to.

Regards,
D
dk is offline   Reply With Quote
Old Feb 22nd, 2002, 08:38 PM   #7
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

Sorry Mudface, have to agree with dk, that procedure runs with Custom Toolbars only.

dk, I'm very Impressed with this. I would change only this:


Set combar = Application.CommandBars("Worksheet Menu Bar")
Set compop = combar.Controls("File")
Set combut = compop.Controls(11)


With this:
Set combut = Application.CommandBars.FindControl(Id:=109)

Because, the 11 Control of File Menu in Excel XP isn't PrintPreview, but the Id, i think, is the same in all versions.

And this


'Now set up print button being pressed
Set combut = compop.Controls(12)


With

Set combut = Application.CommandBars.FindControl(Id:=4)

_________________
Regards,

Juan Pablo G.
MrExcel.com Consulting

[ This Message was edited by: Juan Pablo G. on 2002-02-22 19:38 ]
Juan Pablo González is offline   Reply With Quote
Old Feb 22nd, 2002, 08:43 PM   #8
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

Also, I see that the button in the Standard toolbar is different (The default at least) than the Print Menu in File. The Id for that button is: 2521

It would be just a matter of setting up another button in the class module to handle that one.

Page Setup ID is: 247

I love this class module !
__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 06:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes