PDA

View Full Version : On Key events



lifeson
03-02-2009, 04:20 AM
Hi all
I have a userform that has 7 tabs
Tab 7 contains an imagecontrol that shows various images.
I want a method of printing the image when a user presses Ctrl & p

This identifies that page 7 is active

Private Sub MultiPage1_Change()
If MultiPage1.Value = 7 Then
Application.OnKey "^p", "PrintImage(imgFile)"
End If
End Sub

This should then call this routine
Sub PrintImage(imgFile As String)
'MsgBox "Do you want to print " & imgFile
Application.Worksheets.Add
ActiveSheet.Pictures.Insert(imgFile).Select
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = "Colours may not be as shown"
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.748031496062992)
.RightMargin = Application.InchesToPoints(0.748031496062992)
.TopMargin = Application.InchesToPoints(0.984251968503937)
.BottomMargin = Application.InchesToPoints(0.984251968503937)
.HeaderMargin = Application.InchesToPoints(0.511811023622047)
.FooterMargin = Application.InchesToPoints(0.511811023622047)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -3
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
End With
ActiveWindow.SelectedSheets.PrintPreview
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Delete
End Sub


Do onkey events fire when a user form is open?

Bob Phillips
03-02-2009, 04:23 AM
You cannot pass a parameter via OnKey, you can just fire the procedure. If you want to poass aprameter, you need to load a global variable or some such.

lifeson
03-02-2009, 04:53 AM
Private Sub MultiPage1_Change()
If MultiPage1.Value = 7 Then
MsgBox "page 7 selected"
Application.OnKey "^p", "PrintImage"
End If
End Sub


Thanks XLD for the info on parameters not being able to be passed but I have amended as above and still getting nowhere :doh:

nst1107
03-02-2009, 09:57 AM
http://www.vbaexpress.com/forum/showthread.php?t=25398 Take a look at the VBA in the attachment in post #6. It's a good illustration of what Bob is talking about when he says "load a global variable" to pass to your procedure.