|
-
Mar 11th, 2004, 01:49 PM
#1
Thread Starter
Frenzied Member
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:
For Each OBJ In Printers
Call frmSelPrinter.cboPrinters.AddItem(OBJ.DeviceName)
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".
-
Mar 11th, 2004, 02:00 PM
#2
Lively Member
Printer.DeviceName returns the default printer name.
-
Mar 11th, 2004, 02:06 PM
#3
Thread Starter
Frenzied Member
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".
-
Mar 11th, 2004, 03:54 PM
#4
There is a little more accurate method to determine default printer:
VB Code:
Option Explicit
Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
(ByVal lpAppName As String, ByVal lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long
Public Function GetDefaultPrinter() As String
'=============================================
Dim bfr$, ret&, Pos&
bfr = String$(128, 0)
ret = GetProfileString("WINDOWS", "DEVICE", "", bfr, 127)
bfr = Left(bfr, ret)
Pos = InStr(1, bfr, ",")
GetDefaultPrinter = Left(bfr, Pos - 1)
End Function
'usage:
Private Sub Command1_Click()
'============================
Dim prtName As String
prtName = GetDefaultPrinter
Label1.Caption = "Default Printer: " & prtName
End Sub
Last edited by RhinoBull; Mar 11th, 2004 at 04:03 PM.
-
Sep 6th, 2009, 10:35 AM
#5
Fanatic Member
Re: Detecting Default Printer --> Resolved - Thank you :)
 Originally Posted by RhinoBull
There is a little more accurate method to determine default printer:
VB Code:
Option Explicit Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _ (ByVal lpAppName As String, ByVal lpKeyName As String, _ ByVal lpDefault As String, ByVal lpReturnedString As String, _ ByVal nSize As Long) As Long Public Function GetDefaultPrinter() As String '============================================= Dim bfr$, ret&, Pos& bfr = String$(128, 0) ret = GetProfileString("WINDOWS", "DEVICE", "", bfr, 127) bfr = Left(bfr, ret) Pos = InStr(1, bfr, ",") GetDefaultPrinter = Left(bfr, Pos - 1) End Function 'usage: Private Sub Command1_Click() '============================ Dim prtName As String prtName = GetDefaultPrinter Label1.Caption = "Default Printer: " & prtName End Sub
i dont understand, but is the printer.deviceName not suppose to give this
-
Sep 6th, 2009, 11:33 AM
#6
Re: Detecting Default Printer --> Resolved - Thank you :)
 Originally Posted by coolcurrent4u
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.
-
Sep 6th, 2009, 12:18 PM
#7
Fanatic Member
Re: Detecting Default Printer --> Resolved - Thank you :)
 Originally Posted by RhinoBull
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
-
Sep 6th, 2009, 04:24 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|