Results 1 to 8 of 8

Thread: Print to network printer vb

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651

    Print to network printer vb

    I can print using vb to my local lpt1, using the code below.

    My problem is that now i need to print to a network printer - i need to specify printer name instead of the lpt1.

    How can i do this?

    VB Code:
    1. Open "lpt1" For Output As #1
    2.         For i = 1 To txtPrintNo
    3.             Print #1, strText
    4.         Next i
    Gilly

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Print to network printer vb

    You must use the PRINTER object to print to a network printer.

    You can loop through the PRINTERS collection looking for the desired printer...

    Something like this:

    Code:
    Dim prtPrinter as Printer
    
    For Each prtPrinter In Printers
        If prtPrinter.DeviceName = strDesiredPrinter Then
            Set Printer = prtPrinter
            Exit For
        End If
    Next
    Once you SET PRINTER - then you use the PRINTER object you just assigned to output to that printer.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print to network printer vb

    Is the network printer installed on your machine?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651

    Re: Print to network printer vb

    Yes. It's installed.
    Gilly

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print to network printer vb

    szlamany has already posted what I was going to post about switching printers (providing it was installed).

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651

    Re: Print to network printer vb

    Sorry for being dumb but i can't get this working.

    here is my code at present - any ideas?

    VB Code:
    1. If printerfound Then
    2.         j = Printer.DeviceName
    3.         intFile = FreeFile()
    4.         ' Read Label Template
    5.         Open "c:\label\label.txt" For Input As #intFile
    6.             strText = Input$(LOF(intFile), intFile)
    7.         Close #intFile
    8.         ' Populate with required values
    9.         strText = Replace$(strText, "@MASTERPART@", cboPartNumber.Text)
    10.  
    11. '       Print the required number of Labels
    12.         Open "lpt1" For Output As #1
    13.         For i = 1 To txtPrintNo
    14.             Print #1, strText
    15.        Next i
    16.         Close #intFile
    17.     Else
    18.         MsgBox ("Printer not found.")
    19.     End If
    20.  
    21.  
    22. Dim prtPrinter As Printer
    23.  
    24. For Each prtPrinter In Printers
    25.     If prtPrinter.DeviceName = strDesiredPrinter Then
    26.         Set Printer = prtPrinter
    27.         Exit For
    28.     End If
    29. Next
    30. End Sub
    Gilly

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Print to network printer vb

    You appear to be already using the PRINTER.DEVICENAME at the top of that code - which I'm guessing means you have already determined that the default printer is ok to use.

    This code:

    Code:
    '       Print the required number of Labels
            Open "lpt1" For Output As #1
            For i = 1 To txtPrintNo
                Print #1, strText
           Next i
            Close #intFile
    Changes to:

    Code:
    For i = 1 to txtPrintNo
       Printer.Print strText
    Next i
    Printer.EndDoc
    The loop I gave you was for searching the printer collection for the proper printer - assuming it was not the default printer.

    Hope this helps!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651

    Re: Print to network printer vb

    When i try this the printer icon flickers as though it were in a loop.

    Nothing comes out.

    thanks for ur help...
    Gilly

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