Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning the name of the system default printer to a string variable

Status
Not open for further replies.

enuss

MIS
Mar 16, 2004
37
0
0
US
Hey all,

I'm trying to get the name of my default printer so I can save it into a string variable to use later. I am basically switching back and forth between Adobe PDF and my customer printer, and I don't want to hardcode the name of the printer because I want this to work for people that might not be using the same default printer as my system.

I'm using Access 2000 which does not recognize Application.Printer.DeviceName, Any ideas?

Erich
 
[code--In Form behind button]
Dim oPrn As Printer

' Set the correct printer for this.
For Each oPrn In Printers
If oPrn.DeviceName = PName Then
Set Printer = oPrn
Exit For
End If
Next


[/code]

[code--In Module]


Sub GetPrinterName()
Set FSO = CreateObject("Scripting.FileSystemObject")
sPath = App.Path & "\Printer.txt"
If FSO.fileexists(sPath) Then
Set txtFile1 = FSO.OpenTextFile(sPath, 1, False)
On Error GoTo ExitErr

Do While Not txtFile1.AtEndOfStream

PName = txtFile1.readline

Loop

End If
ExitErr:
txtFile1.Close

End Sub
[/code]

I've found that by omitting the PName reference that it automatically picks up the default printer, but I'm more into .Net.

I hope this helps.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
enuss,
I don't know if this is what your looking for. This works on my machine running Windows XP Professional SP2 and the code was testing in Excel 2000 SR-1.
Code:
Public Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal pszBuffer As String, pcchBuffer As Long) As Long

Function DefaultPrinterName() As String
Dim lngBufferLen As Long, lngReturn As Long
Dim strBuffer As String

'First call results in error but returns buffer length needed
lngReturn = GetDefaultPrinter(strBuffer, lngBufferLen)
If lngReturn = 0 Then
  'Size the buffer
  strBuffer = Space(lngBufferLen)
  'call again to get the value
  lngReturn = GetDefaultPrinter(strBuffer, lngBufferLen)
  If lngReturn <> 0 Then
    DefaultPrinterName = Left(strBuffer, lngBufferLen - 1)
    Exit Function
  End If
End If
MsgBox "No default printer was retunred", vbCritical, "API error"
End Function

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top