CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2000
    Posts
    149

    Use VB to send file to printer...

    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



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Use VB to send file to printer...

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Feb 2000
    Posts
    149

    Re: Use VB to send file to printer...

    Many thanks Iouri!!!


  4. #4
    Join Date
    Feb 2000
    Posts
    149

    Re: Use VB to send file to printer...

    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?


  5. #5
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Use VB to send file to printer...

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  6. #6
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Use VB to send file to printer...

    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






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured