by loquin(Legacy Member) on Thu Jun 15, 2006 3:01 pm
Here's the code I've used for this task.
Essentially, I place labels, textboxes, images, pictures & lines in the positions required, and with the fonts required.
Then, I load a new instance of the form, Populate the form objects with the data I need to print, and then call PrintMyForm
Useage:
Dim f as MyFormName
Set f = New MyFormName
Load f
' Populate the form. You don't have to show it if you don't want to.
PrintMyForm f
Printer.EndDoc ' Advance to the next label
'********* Place the following code in a module so it's accessable from anywhere in your app
Public Sub PrintMyForm(theForm As Form)
' Requires that the form be loaded.
Dim obj As Object
For Each obj In theForm
If obj.Visible Then
PrintObject obj
End If
Next obj
End Sub
Public Sub PrintObject(theObj As Object)
' works with lines, pictureboxes, Images, textboxes, and labels.
If TypeOf theObj Is Line Then
PrintLine theObj
End If
If TypeOf theObj Is TextBox Then
PrintTextBox theObj
End If
If TypeOf theObj Is Label Then
PrintLabel theObj
End If
If TypeOf theObj Is PictureBox Then
PrintPicture theObj
End If
If TypeOf theObj Is Image Then
PrintImage theObj
End If
End Sub
Public Sub PrintTextBox(tTextBox As TextBox)
' Handles justification, fonts, etc.
With tTextBox
Printer.ForeColor = .ForeColor
Printer.Font = .Font
Printer.Font.Name = .Font.Name
Printer.Font.Charset = .Font.Charset
Printer.Font.Bold = .Font.Bold
Printer.Font.Italic = .Font.Italic
Printer.Font.Size = .Font.Size
Printer.Font.Strikethrough = .Font.Strikethrough
Printer.Font.Underline = .Font.Underline
Printer.Font.Weight = .Font.Weight
Select Case .Alignment
Case vbCenter
Printer.CurrentX = .Left + (.Width - Printer.TextWidth(.Text)) / 2
Case vbLeftJustify
Printer.CurrentX = .Left
Case vbRightJustify
Printer.CurrentX = .Left + .Width - Printer.TextWidth(.Text)
End Select
Printer.CurrentY = .Top
Printer.Print .Text
End With
End Sub
Public Sub PrintLabel(tLabel As Label)
' Handles justification, fonts, etc.
Dim objFont As Object
Dim sFont As New StdFont
Dim bcFont As New StdFont
Dim N As Integer
sFont.Name = "MS Sans Serif"
With tLabel
Printer.ForeColor = .ForeColor
Set Printer.Font = tLabel.Font
Printer.Font.Name = tLabel.Font.Name
Printer.Font.Charset = .Font.Charset
Printer.Font.Bold = .Font.Bold
Printer.Font.Italic = .Font.Italic
Printer.Font.Size = .Font.Size
Printer.Font.Strikethrough = .Font.Strikethrough
Printer.Font.Underline = .Font.Underline
Printer.Font.Weight = .Font.Weight
Select Case .Alignment
Case vbCenter
Printer.CurrentX = .Left + (.Width - Printer.TextWidth(.Caption)) / 2
Case vbLeftJustify
Printer.CurrentX = .Left
Case vbRightJustify
Printer.CurrentX = .Left + .Width - Printer.TextWidth(.Caption)
End Select
Printer.CurrentY = .Top
Printer.Print .Caption
End With
Printer.Font = sFont
Printer.Font.Name = "MS Sans Serif"
Printer.Font.Size = 10
End Sub
Public Sub PrintPicture(tPicBox As PictureBox)
With tPicBox
Printer.PaintPicture .Image, .Left , .Top , .Width, .Height
End With
End Sub
Public Sub PrintImage(tImgBox As Image)
With tImgBox
Printer.PaintPicture .Picture, .Left, .Top, .Width, .Height
End With
End Sub
Public Sub PrintLine(tLine As Line, Optional DrawWidth As Integer = 10)
With tLine
Printer.DrawWidth = DrawWidth
Printer.Line (.X1, .Y1)-(.X2, .Y2)
End With
End Sub