Hiya
Anyone know how I can print the contents of a listbox line by line, nothing fancy, just plain text. I would like to have the Printer dialog come up as well if that is possible.
Thank you!
Printable View
Hiya
Anyone know how I can print the contents of a listbox line by line, nothing fancy, just plain text. I would like to have the Printer dialog come up as well if that is possible.
Thank you!
Code:Private Sub Command1_Click()
Dim iList As Integer
Dim sList As String
For iList = 0 to List1.ListCount - 1
sList = sList & List1.List(iList) & vbCrLf
Next iList
If Msgbox("Would you like to print?", vbOKCancel) = vbOK Then
Printer.Print sList
Printer.EndDoc
Else
Msgbox "Printing canceled."
End If
End Sub
Code:Option Explicit
'
'API function to call the printer dialog box
'
Private Declare Function PrintDlg Lib "comdlg32.dll" _
Alias "PrintDlgA" (pPrintdlg As PRINTDLGS) As Long
Private Type PRINTDLGS
lStructSize As Long
hwndOwner As Long
hDevMode As Long
hDevNames As Long
hDC As Long
flags As Long
nFromPage As Integer
nToPage As Integer
nMinPage As Integer
nMaxPage As Integer
nCopies As Integer
hInstance As Long
lCustData As Long
lpfnPrintHook As Long
lpfnSetupHook As Long
lpPrintTemplateName As String
lpSetupTemplateName As String
hPrintTemplate As Long
hSetupTemplate As Long
End Type
Private Sub Command1_Click()
Dim PrintStruct As PRINTDLGS
PrintStruct.lStructSize = Len(PrintStruct)
Call PrintDlg(PrintStruct)
With List1
For i = 0 To .ListCount - 1
Printer.Print List1.List(i)
Next
End With
Printer.EndDoc 'start printer
End Sub
Thanks guys! Swift response...however HeSaidJoe, it just prints to the default printer, no matter which printer you select in the dialog!
Sorry, kicked out early and gone fishing all day.
Try changing your command1 code to this...actually just added one new line but I only have one printer so I can't test it.
Code:Private Sub Command1_Click()
Dim PrintStruct As PRINTDLGS
PrintStruct.lStructSize = Len(PrintStruct)
Call PrintDlg(PrintStruct)
Set Printer = Printer.DeviceName
With List1
For i = 0 To .ListCount - 1
Printer.Print List1.List(i)
Next
End With
Printer.EndDoc 'start printer
End Sub
Thanks ...Joe but I already tried that one and it either didn't make any difference, or just plan blew up...Object Required apparently.
Doh!