Results 1 to 3 of 3

Thread: How can you set a different printer than default one ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    66

    Question

    What I would like to do is to change the default printer by another one before calling the ShowPrinter.
    Here is my code, but when I call the printer dialog box, the printer specified is the default printer but not the one I selected.

    Printer_Name is a global which contains the printer I want to select (\\SERVER\PRINTERNAME)

    Dim prt As Printer
    For Each prt In Printers
    If LCase(prt.DeviceName) = LCase(Printer_Name) Then
    Set Printer = prt
    GoTo PrintDialog
    End If
    Next prt
    MsgBox "Printer not define: " & Printer_Name, vbCritical
    Exit Sub
    PrintDialog:
    CMDialog.ShowPrinter


    Thanks for your help !...
    Thierry

    [email protected]

  2. #2
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334
    This should get you started on iterating the printers collection - it is a little specific to our setup but you should get the idea

    Code:
    Public Function GetPrinters()
     
        '------------------------------
        'Function Description
        'Set up printers _
        Use Printers collection _
        to loop through and add the printer _
        to the list in dlgPrinters.
        
        'When this is done then loop the new list _
        and identify the current printer and select it _
        so then the user sees their currently defaulted _
        printer.
        
        
    
     
     Dim iList%, obj As Printer, iSLen%, sPrint$, iIndex%
           
        
        'Loop the printers collection
        For Each obj In Printers
            
            iSLen = InStr(UCase(Printer.DeviceName), "PPMA")
            sPrint = UCase(obj.DeviceName)
                  
            dlgPrinters.lstPrint.AddItem UCase(Mid(sPrint, iSLen, 9)) 'Add to the listbox
            
        Next
           
        'Loop the list box to find the current printer
        For iList = 1 To dlgPrinters.lstPrint.ListCount
            dlgPrinters.lstPrint.ListIndex = iList - 1
            If UCase(dlgPrinters.lstPrint.Text) = Mid(UCase(Printer.DeviceName), iSLen, 9) Then
                iIndex = dlgPrinters.lstPrint.ListIndex 'Bookmark the current printer
                Exit For
            End If
        Next
            
            If dlgPrinters.lstPrint.ListCount > 0 Then
                dlgPrinters.lstPrint.ListIndex = (iIndex) ' Set the current item in the listbox
            Else
                Exit Function
            End If
            
    
    End Function
    This next bit sets the printer you require - if it already in the collection - research the API call AddPrinterConnection to see how to add a printer to the collection.

    Code:
    Private Sub cmdSet_Click()
        
        Dim obj As Printer, sPrinter$, sNewPrinter$, iInd%
    
        sOldPrinter = Printer.DeviceName
        
        sPrinter = lstPrint.Text
        
        iInd = 0
        Dim iLen%, iSLen%, sPrin$
        For Each obj In Printers
            sPrin = obj.DeviceName
            iSLen = InStr(UCase(sPrin), "PPMA")
            
            
            'if PPMA present then use this in the list
            If Mid(UCase(sPrin), iSLen, 9) = UCase(sPrinter) Then
                'Debug.Print obj.DeviceName
                sNewPrinter = Mid(UCase(sPrin), iSLen, 9)
                Set Printer = obj
                Exit For
                GoTo Update
            End If
        Next
        
    Update:
            
        Set obj = Nothing
        
        Unload Me
        
    End Sub
    Again tweaked to suit the setup here but the necessaries are all there.
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    66

    Angry This does not solve my problem

    First of all, before changing anything, if I display the 'Printer.DeviceName', I got for example :

    \\SERVER1\Default Printer Name

    Then, after executing my code and setting the printer I want to select as follow :

    Set Printer = prt

    Then, if I display the 'Printer.DeviceName', I got for example :

    \\SERVER2\Selected Printer Name

    And this is correct !...
    But at this moment, if I type the command :

    CMDialog.ShowPrinter

    The dialog box is set on \\SERVER1\Default Printer Name instead of \\SERVER2\Selected Printer Name

    In fact, I would like to change the default printer of the printer dialog box without calling it.

    ====================================

    I have created a procedure which allows me to determine the correct font to print a file depending on the number of lines per page and a number of characters per lines.
    For this, I call the CMDialog box to select the printer, then I use the different API CreateFontIndirect, GetExtentPoint32 and GetTextMetrics to do so.

    And it works perfectly !... But I had to use the hDc property of the CMDialog to make it works.

    Now, I just want to perform the same code without CMDialog box. Then I used the Printer.hDc but it does not work !....

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