Results 1 to 12 of 12

Thread: printing to a certain printer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    Israel the holy land
    Posts
    160

    printing to a certain printer

    i have this code :
    VB Code:
    1. Sub Command1_Click()
    2.  
    3. Set IESource = New InternetExplorer
    4. Set IETarget = New InternetExplorer
    5. With IESource
    6.       .navigate "http://www.walla.co.il"
    7.       .Visible = True
    8. End With
    9.  
    10. End Sub
    11.  
    12. Private Sub IESource_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    13. If (pDisp Is IESource) Then
    14.     Dim a As HTMLAnchorElement
    15.    
    16.     IESource.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    17.     For Each a In IESource.document.anchors
    18.         bolContinue = False
    19.         IETarget.navigate a.href
    20.         Do While Not bolContinue
    21.             DoEvents
    22.         Loop
    23.        
    24.     Next
    25. End If
    26.  
    27. End Sub
    which prints a loaded html page into default printer!
    how an i tell it to print to another printer that is installed and connected?
    thnaks i nadvance
    peleg
    Israel - the best place to live in after heaven, but no one want's to go there so fast.

    http://www.networked-toys.com/
    http://www.nirlat.com/home_page.asp

  2. #2
    New Member
    Join Date
    Jan 2005
    Posts
    10

    Re: printing to a certain printer

    Use common dialog control... put one of the form name is cd1 then simple before the print out actually takes place enter cd1.showprinter select the printer you want to print to then click print.

    Take care

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    Israel the holy land
    Posts
    160

    Re: printing to a certain printer

    but all the point is hat i DONT!!! want to promt the user for the printer
    i want to put it by myself!

    by the way : i didnt understand execlly your suggestion , so just for knowledge can you explain execly what to do?
    thnaks in advance
    peleg
    Israel - the best place to live in after heaven, but no one want's to go there so fast.

    http://www.networked-toys.com/
    http://www.nirlat.com/home_page.asp

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: printing to a certain printer

    whether you use the commondialog or not you still have to withe the code

    the basic idea is to loop through all the printers available to select the one you want

    Dim X As Printer
    For Each X In Printers
    If X.Orientation = vbPRORPortrait Then
    ' Set printer as system default.
    Set Printer = X
    ' Stop looking for a printer.
    Exit For
    End If
    Next

    depanding on how you want to select the printer you want use, you might use if x.devicename = "myprintername" then set printer = X

    hope this helps pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    Israel the holy land
    Posts
    160

    Re: printing to a certain printer

    will this line :
    VB Code:
    1. Set Printer = X
    will set the default printer only for the time the exe is running
    or it will change the computer default printer - as like i myself and cahnged the default printer?
    thnaks in advance
    peleg
    Israel - the best place to live in after heaven, but no one want's to go there so fast.

    http://www.networked-toys.com/
    http://www.nirlat.com/home_page.asp

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: printing to a certain printer

    this changes the printer for your program

    it does not change the windows default printer for all programs,
    you should have no need to change it back

    pete

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: printing to a certain printer

    Sorry if your printing is actually done by internet explorer, then you will need to change the windows default printer for the duration of your printing, then change back afterwards

    pete

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: printing to a certain printer

    to change default printer is different in windows 9x to XP / NT windows

    in sub
    VB Code:
    1. p1 = Printer.DeviceName
    2. p2 = Printer.DeviceName & "," & Printer.DriverName & "," & Printer.Port
    3. For Each p3 In Printers
    4.     If Left(p3.DeviceName, 11) = "ElectraSoft" Then Exit For       'p.DeviceName
    5. Next
    6. SetDefaultPrinter p3.DeviceName       'for windows 9x
    7. p4 = p3.DeviceName & "," & p3.DriverName & "," & p3.Port
    8.  
    9.     r = WriteProfileString("windows", "Device", p4)    'for XP/NT

    for XP/Nt you need the writeprofilestring api
    for windows 9x you need all the other code below, which i have in a module

    VB Code:
    1. Type PRINTER_INFO_5
    2.   pPrinterName As String
    3.   pPortName As String
    4.   Attributes As Long
    5.   DeviceNotSelectedTimeout As Long
    6.   TransmissionRetryTimeout As Long
    7.  
    8. End Type
    9.  
    10. ''''''Type PRINTER_INFO_3
    11. ''''''pSecurityDescriptor As SECURITY_DESCRIPTOR
    12. ''''''End Type
    13. Public Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
    14. Public Const PRINTER_ACCESS_ADMINISTER As Long = &H4
    15. Public Const PRINTER_ACCESS_USE As Long = &H8
    16.  
    17. Public Const PRINTER_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
    18. Public Const PRINTER_ATTRIBUTE_DEFAULT As Long = &H4
    19.  
    20.  Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, hPrinter As Long, ByVal pDefault As Long) As Long
    21.  Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, lpPrinter As Any, ByVal Command As Long) As Long
    22.  Declare Function ClosePrinter Lib "winspool.drv" (ByVal hKey As Long) As Long
    23.  
    24.  
    25.  
    26. Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
    27.  
    28.  
    29.  
    30.  
    31.  
    32. Function SetDefaultPrinter(pname) As Integer
    33.  
    34.  
    35. Dim lpPrinter As PRINTER_INFO_5, dwLevel&, dwCommand&, Res&
    36. Dim iOK&, hKey&, p As Printer
    37. 'Const PRINTER_ATTRIBUTE_DEFAULT As Long = &H4
    38. For Each p In Printers
    39. If p.DeviceName = pname Then Exit For
    40. Next
    41.   dwLevel = 5
    42.   dwCommand = 1
    43.   With lpPrinter
    44.     .pPrinterName = pname
    45.     .pPortName = p.Port
    46.     .Attributes = PRINTER_ALL_ACCESS 'PRINTER_ATTRIBUTE_DEFAULT
    47.     .DeviceNotSelectedTimeout = 15
    48.     .TransmissionRetryTimeout = 45
    49.   End With
    50.   ' ** If return value = 0 then function call generally unsuccessful  **
    51.   Res = OpenPrinter(lpPrinter.pPrinterName, hKey, 0)
    52.   If Res <> 0 Then
    53.     iOK = SetPrinter(hKey, dwLevel, lpPrinter, dwCommand)
    54.   End If
    55.   Res = ClosePrinter(hKey)
    56.   If iOK <> 0 Then
    57.     SetDefaultPrinter = -1    ' True
    58.   End If
    59. End Function

    i think i copied everything in, cos i had a lot of other stuff in that module
    don't forget to declare all variables as required

    rgds pete

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    Israel the holy land
    Posts
    160

    Re: printing to a certain printer

    there is a problem with
    VB Code:
    1. p1 = Printer.DeviceName
    2. p2 = Printer.DeviceName & "," & Printer.DriverName & "," & Printer.Port
    3. For Each p3 In Printers
    4.     If Left(p3.DeviceName, 11) = "ElectraSoft" Then Exit For       'p.DeviceName
    5. Next
    6. SetDefaultPrinter p3.DeviceName       'for windows 9x
    7. p4 = p3.DeviceName & "," & p3.DriverName & "," & p3.Port
    8.  
    9.     r = WriteProfileString("windows", "Device", p4)    'for XP/NT
    beacuse if you exit the loop p3 looses is data
    Israel - the best place to live in after heaven, but no one want's to go there so fast.

    http://www.networked-toys.com/
    http://www.nirlat.com/home_page.asp

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: printing to a certain printer

    He does an exit so that P3 DOES NOT lose its data.
    If it finished, it would lose the correct value.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    Israel the holy land
    Posts
    160

    Re: printing to a certain printer

    well i debuged it
    found the printer i want and when i go to the SetDefaultPrinter line
    i get an error and check and see that p3 is empty
    Israel - the best place to live in after heaven, but no one want's to go there so fast.

    http://www.networked-toys.com/
    http://www.nirlat.com/home_page.asp

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: printing to a certain printer

    Here is my code, called from clicking on a text box.

    VB Code:
    1. Private Sub txtPrinter_Click()
    2.   Dim prtPrinter As Printer
    3.   Dim oCDBForm As New frmCommonDialog
    4.   x = lblSetup(1).Left + lblSetup(1).Width
    5.   y = lblSetup(1).Top
    6.   oCDBForm.Move x, y
    7.   With oCDBForm.CommonDialog1
    8.     .DialogTitle = "Select Printer to use"
    9.     .Flags = cdlPDNoPageNums _
    10.            Or cdlPDNoSelection _
    11.            Or cdlPDPrintSetup _
    12.            Or cdlPDReturnDC
    13.     .ShowPrinter
    14.     txtPrinter.Text = Printer.DeviceName
    15.   End With
    16.  
    17.   For Each prtPrinter In Printers
    18.     If prtPrinter.DeviceName <> txtPrinter.Text Then
    19.         Set Printer = prtPrinter
    20.         txtPrinter.Text = prtPrinter.DeviceName
    21.         Exit For
    22.     End If
    23.   Next
    24.   Unload oCDBForm
    25.   Set oCDBForm = Nothing
    26. End sub

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