Results 1 to 8 of 8

Thread: Getdefaultprinter problems

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Getdefaultprinter problems

    GetDefaultPrinter and SetDefaulPrinter Problem

    I am having a problem with the above features in a vb6 program in Win10.

    I am writing a program in which I switch kitchen printers in a a restaurant depending on what has ben ordered (pizza at 1, burgers to another, etc.)

    When I try to switch printers using setdefaultprinter and getdefaultprinter these features do not work all the time. MS says there is a problem in previous versions such as win8 but since vb6 in win10 is not supported I am not sure what hotfix, if any, I should use.


    The following is the area of the problem. The '<<<< lines are the problem
    'This is printing for drinks
    Dim oldPrinter As String
    Printer.FontBold = True
    Printer.FontSize = 14
    If NumDrinks > 0 Then
    GoSub Headingx
    oldPrinter = GetDefaultPrinter '<<<<<<<<<<<<<<
    SetDefaultPrinter DrinkOrderPrinter <<<<<<<<<<<<<<<<<<<
    Printer.Print sServerName '( 'SERVERNUMBER)
    For XXX = 1 To NumDrinks
    Printer.Print PrintDrinkItems(XXX)
    Next XXX
    Printer.EndDoc
    End If

    'THIS IS FOR APPS ONLY PRINTING
    If NumApps > 0 And NumMains = 0 Then
    GoSub Headingx
    oldPrinter = GetDefaultPrinter '<<<<<<<<<<<<<<<<<<<<<<<
    SetDefaultPrinter FoodOrderPrinter '<<<<<<<<<<<<<<<<<<<<<<<<<
    For XXX = 1 To NumPrintedItems
    Printer.Print PrintItems(XXX)
    Next XXX
    Printer.EndDoc
    End If

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

    Re: Getdefaultprinter problems

    features do not work all the time
    does it work some times and not others?, i tested in W10, appeared to work correctly
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: Getdefaultprinter problems

    Quote Originally Posted by westconn1 View Post
    does it work some times and not others?, i tested in W10, appeared to work correctly
    Yes, it is very erratic. sometime work sometimes doesn't. Mostly it doesn't.

    Is it possible to see what your coding looks like?

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

    Re: Getdefaultprinter problems

    I created a new project, form with one combo box

    Code:
    Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
       Alias "SetDefaultPrinterA" _
      (ByVal pszPrinter As String) As Long
    Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
    
     Dim pn As String  ' keep original default printer to reset after testing
    
    Private Sub Combo1_Click()
    SetDefaultPrinter Combo1.List(Combo1.ListIndex)
    
    End Sub
    
    Private Sub Form_Load()
    Dim p As Printer
    pn = defprinter
    Caption = pn
    
    For Each p In Printers
        Combo1.AddItem p.DeviceName
    Next
    Combo1.Text = pn
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    If Not IsEmpty(pn) Then SetDefaultPrinter pn
    End Sub
    Function defprinter() As String
    Dim sLen As Long, iPrn As Printer, hResult As Long
    
    defprinter = Space$(255)
    sLen = 255
    hResult = GetDefaultPrinter(ByVal defprinter, sLen)
    If hResult <> 0 Then defprinter = Left(defprinter, sLen - 1)
    
    End Function
    anyway, I do not believe you should be changing the default printer, for what you are wanting to do, just change the vb application printer, not the windows default printer, this may also be causing the problem you are having with setdefaultprinter, as vb is likely to continue using the same printer as what the default printer was when the application started, even if the default printer is changed while the application is running

    just change the printer for your vb application will print to, like below, you can use a combobox (as above) for the user to select which printer they want
    Code:
    for each p in printers
        if p.devicename = "DrinksPrinter" then set printer = p: exit for
    next
    ' if you are using a combobox, you can replace the above with
    set printer = combo1.listindex
    with printer
       .font.name = "Arial"
       .font.size = 14
    end with
    printer.print "Hello work"
    printer.print "Next line"
    printer.enddoc
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: Getdefaultprinter problems

    Westconn1:
    I suspect the answer is in your last post I want but not sure where.
    I am running out of time on this project.
    I would like to hire you.
    BILL MILLER
    URSA MAJOR
    1 585 872 3451
    WWWURSA@GMAIL.COM

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: Getdefaultprinter problems

    think I have figured out what is happening. The menu items are printing out on the previous printer. If i had a drink order and a appetizer order and a main course order the drinks will come out on the printer last selected from the previous order, the appetizers will come out on the drink order and the main courses come out on the appetizer printer

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

    Re: Getdefaultprinter problems

    are you still using setdefault printer to change printers?

    what is the criteria (how are you specifying) to select the printer you want for each menu type?
    how is the user to choose which printer, or is it to be automatically selected by the menu item type (maybe a field in the database)?
    how are the printer names organized?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,238

    Re: Getdefaultprinter problems

    I have many app that may need to print something. Its not easy finding the printer without the Common Dialog or using a Combo box to find all installed printers.

    I use Printer.DeviceName which uses the default printer. I ask the user to look at printer and make the one they want to use. Then it prints to the default printer no problem.

    If it shows an error 484 there is no printer installed.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

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