Click to See Complete Forum and Search --> : Use VB to send file to printer...


Dark Sean
March 15th, 2001, 01:00 PM
Hi,
this may sound like a simple question, but what is the command to send a file to a printer? I'm trying to use the common dialog box first to choose the network printer to print to, and then use the Printer Object like this...

Printer.print 'FileName




It doesn't seem to want to send the file to the printer, do I have to send the file record by record? Please tell me I can just send the damned file!!!

Thanks,
Sean

Iouri
March 15th, 2001, 01:12 PM
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Dim lngResult As Long
lngResult = ShellExecute(Me.hwnd,"Print",strFile,0&,0&,vbMinimized)

where strFile is the full path to your file





Iouri Boutchkine
iouri@hotsheet.com

Dark Sean
March 15th, 2001, 01:14 PM
Many thanks Iouri!!!

Dark Sean
March 15th, 2001, 01:30 PM
Wait a second, how do I specify which printer I am printing to with this??? Here is my cmdPrint code:


Dim ReturnVal as string
Dim strFile as string
Dim lngResult as Long

ReturnVal = Dir(LABELDIRECTORY & strOrder & ".d00")
If ReturnVal = strOrder & ".d00" then
ReturnVal = MsgBox("is the printer set up correctly?", vbYesNo, "LabelMaker")
If ReturnVal = vbNo then
Exit Sub
End If


cdlLabel.ShowPrinter

strFile = LABELDIRECTORY & strOrder & ".d00"

lngResult = ShellExecute(me.hwnd, "print", strFile, 0&, 0&, vbMinimized)



MsgBox "Labels have begun to print..."

End If




Nothing happens....I use the ShowPrinter method of the coommon dialog box to specify printer, and then used ShellExecute.

Ideas?

Iouri
March 15th, 2001, 01:44 PM
It will print to the default printer.
If you want to change the printer

Here the code

Private Function SelectPrinter(ByVal printer_name As String) As Boolean
Dim i As Integer

SelectPrinter = True
For i = 0 To Printers.Count - 1
If Printers(i).DeviceName = printer_name Then
Set Printer = Printers(i)
SelectPrinter = False
Exit For
End If
Next i
End Function


Private Sub Form_Load()
Dim i As Integer

cboPrinter.Clear
For i = 0 To Printers.Count - 1
cboPrinter.AddItem Printers(i).DeviceName
Next i
End Sub

'choose in combo printer_name


Iouri Boutchkine
iouri@hotsheet.com

shree
March 15th, 2001, 08:45 PM
Depends upon the formfat of your file. The ShellExecute will work if your file is a registered type. Here is a code sample that should work (I haven't tested it though). It will print to the printer you select with a CommonDialog Box.


'Printing a file involves opening the file, reading into a byte array,
'converting from Unicode format to ASCII and sending the converted string to the printer.

dim FileHandle as Integer
Dim FileAsByte() as Byte
Dim FileAsStr as string
FileHandle = FreeFile
Open "Filename.ext" for binary as FileHandle
ReDim FileAsByte(LOF(FileHandle))
get PPTFileHandle, , FileAsByte
FileAsStr = StrConv(FileAsByte(), vbUnicode)
Close PPTFileHandle
Printer.print FileAsStr
FileAsStr = ""
Redim FileAsByte(1)
End Function