Results 1 to 8 of 8

Thread: Detecting Default Printer --> Resolved - Thank you :)

  1. #1

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Question Detecting Default Printer --> Resolved - Thank you :)

    I have this code in which I read all the printers on the PC, it works just fine..

    VB Code:
    1. For Each OBJ In Printers
    2.     Call frmSelPrinter.cboPrinters.AddItem(OBJ.DeviceName)
    3. Next

    My question is once I have done this and I have all the printers loaded into my combo box I want to set the index to the one that is the default printer. However I do not know how to detect which one is the default printer.. Can some one please help meout with this?

    Thank you

    Rudy
    Last edited by RudyL; Mar 11th, 2004 at 02:06 PM.
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  2. #2
    Lively Member
    Join Date
    Aug 2003
    Location
    Philadelphia, Pa.
    Posts
    123
    Printer.DeviceName returns the default printer name.

  3. #3

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Originally posted by Smeagol
    Printer.DeviceName returns the default printer name.
    hehe.. Right in front of my face! Thank Smeagol!


    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    There is a little more accurate method to determine default printer:
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
    3.                 (ByVal lpAppName As String, ByVal lpKeyName As String, _
    4.                 ByVal lpDefault As String, ByVal lpReturnedString As String, _
    5.                 ByVal nSize As Long) As Long
    6.  
    7.  
    8. Public Function GetDefaultPrinter() As String
    9. '=============================================
    10. Dim bfr$, ret&, Pos&
    11.  
    12.     bfr = String$(128, 0)
    13.     ret = GetProfileString("WINDOWS", "DEVICE", "", bfr, 127)
    14.     bfr = Left(bfr, ret)
    15.     Pos = InStr(1, bfr, ",")
    16.     GetDefaultPrinter = Left(bfr, Pos - 1)
    17.  
    18. End Function
    19.  
    20. 'usage:
    21. Private Sub Command1_Click()
    22. '============================
    23. Dim prtName As String
    24.  
    25.     prtName = GetDefaultPrinter
    26.     Label1.Caption = "Default Printer: " & prtName
    27.  
    28. End Sub
    Last edited by RhinoBull; Mar 11th, 2004 at 04:03 PM.

  5. #5
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Detecting Default Printer --> Resolved - Thank you :)

    Quote Originally Posted by RhinoBull View Post
    There is a little more accurate method to determine default printer:
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
    3.                 (ByVal lpAppName As String, ByVal lpKeyName As String, _
    4.                 ByVal lpDefault As String, ByVal lpReturnedString As String, _
    5.                 ByVal nSize As Long) As Long
    6.  
    7.  
    8. Public Function GetDefaultPrinter() As String
    9. '=============================================
    10. Dim bfr$, ret&, Pos&
    11.  
    12.     bfr = String$(128, 0)
    13.     ret = GetProfileString("WINDOWS", "DEVICE", "", bfr, 127)
    14.     bfr = Left(bfr, ret)
    15.     Pos = InStr(1, bfr, ",")
    16.     GetDefaultPrinter = Left(bfr, Pos - 1)
    17.  
    18. End Function
    19.  
    20. 'usage:
    21. Private Sub Command1_Click()
    22. '============================
    23. Dim prtName As String
    24.  
    25.     prtName = GetDefaultPrinter
    26.     Label1.Caption = "Default Printer: " & prtName
    27.  
    28. End Sub
    i dont understand, but is the printer.deviceName not suppose to give this
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Detecting Default Printer --> Resolved - Thank you :)

    Quote Originally Posted by coolcurrent4u View Post
    i dont understand, but is the printer.deviceName not suppose to give this
    Not really. Try this:
    Code:
    Private Sub Command1_Click()
    Dim prt As Printer
    
        For Each prt In Printers
            Debug.Print prt.DeviceName
        Next prt
    
    End Sub
    After running that simple snippet you will get list of all printers currently available on your system.
    But DeviceName cannot tell you which one is your default.

  7. #7
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Detecting Default Printer --> Resolved - Thank you :)

    Quote Originally Posted by RhinoBull View Post
    Not really. Try this:
    Code:
    Private Sub Command1_Click()
    Dim prt As Printer
    
        For Each prt In Printers
            Debug.Print prt.DeviceName
        Next prt
    
    End Sub
    After running that simple snippet you will get list of all printers currently available on your system.
    But DeviceName cannot tell you which one is your default.
    you dont have to loop, just call printer.devicename and it will give you the default. i have tried this many times
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

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

    Re: Detecting Default Printer --> Resolved - Thank you :)

    you dont have to loop, just call printer.devicename and it will give you the default. i have tried this many times
    if the current printer for your application is not the default printer, then it will not return the default printer, but the printer your program is using
    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

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