Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A better way to print a form.

What VB shoud be able to do!

A better way to print a form.

by  Michael42  Posted    (Edited  )
[color green]'For printCurrentForm[/color]
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const VK_MENU As Byte = &H12
Const VK_SNAPSHOT As Byte = &H2C
Const KEYEVENTF_KEYUP = &H2


Public Function printCurrentForm(pForm As Form)
On Error GoTo ErrorHandler

Dim lWidth As Long, lHeight As Long

Clipboard.Clear
Call keybd_event(VK_MENU, 0, 0, 0)
Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
DoEvents

Call keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)

Printer.Print
If pForm.Width > Printer.ScaleWidth Then
lWidth = Printer.ScaleWidth
lHeight = (Printer.ScaleWidth / pForm.Width) * pForm.Height
Else
lWidth = pForm.Width
lHeight = pForm.Height
End If
Printer.PaintPicture Clipboard.GetData, 0, 0, lWidth, lHeight
Printer.EndDoc

DoEvents
'Clipboard.Clear
' SavePicture Clipboard.GetData, Me.Name & ".BMP"

On Error GoTo 0
Exit Function


ErrorHandler:
MsgBox "Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description, vbExclamation, "Error (printCurrentForm)"

End Function

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top