Attempting to retrieve all printer names using module function

  • Thread starter Thread starter Brian G via AccessMonster.com
  • Start date Start date
B

Brian G via AccessMonster.com

Forgive my noobiness but can't seem to get this function to funciton (heh, a
little noob humour).

I am calling this from a form to grab all of the printer names and save them
to a small table.

Function ShowPrinters() As String
Dim strPrinters As String
Dim lngLen As Long

Dim prn As Printer
For Each prn In Application.Printers
strPrinters = strPrinters & prn.DeviceName & ";"
Next

lngLen = Len(strPrinters) - 1 'without trailing ";".
If lngLen > 0 Then
strPrinters = Left(strPrinters, lngLen)
ShowPrinters = strPrinters
End If


End Function

I cobbled this thing together from a couple of posts forgive my wrecklessness.
 
sorry, this seems to happen everytime I post here, I spend three hours trying
to get something to work, give up and post a question and then figure it out
10 minutes later

the solution

Function ShowPrinters() As String
Dim strPrinters As String
Dim lngLen As Long

Dim prn As Printer
For Each prn In Application.Printers
strPrinters = strPrinters & prn.DeviceName & ";"
Next

lngLen = Len(strPrinters) - 1 'without trailing ";".
If lngLen > 0 Then
strPrinters = Left(strPrinters, lngLen)
ShowPrinters = strPrinters

End If

End Function

Seems to work after all, there was some issue with the variable which seems
to be working now
 
and the embarassment continues....

it seems to run fine on machines running xp pro, but the two systems I check
it on that use xp home balk at this line

Dim prn As Printer

saying
Compile Error:
User-defined not defined

anyone?
 
On XP Home, Open the database, then

[ctrl][g]
Tools,References

Check for any missing or broken references.

If there are no missing or broken references, add any new reference, close
the dialog, then re-open the dialog, remove the new reference, and check
again for any broken or missing reference.

(david)
 
Brian G via AccessMonster.com wrote in message said:
and the embarassment continues....

it seems to run fine on machines running xp pro, but the two systems I check
it on that use xp home balk at this line

Dim prn As Printer

saying
Compile Error:
User-defined not defined

anyone?

Would it by any chance also be different Access version? I think the
printers
collection became available in the 2002 (xp) version of Access.
 
..... of course you're right, those machines are running Access 2000, although
it says it's in 2000 version I did build it and intially ran it on Access
2003.

Is there a way to make it work in 2000?
 
Thanks Roy,
okay, found a few things and I am nearly there...

I can get the printer names in the immediate window but haven't the knowledge
to transfer the info properly

first set this up in it's own module
------------------------------------------------------------------------------
-----------
Option Compare Database
Private Const PRINTER_ENUM_LOCAL = &H2
Private Const PRINTER_ENUM_CONNECTIONS = &H4

Private Declare Function EnumPrinters Lib "winspool.drv" _
Alias "EnumPrintersA" _
(ByVal flags As Long, _
ByVal name As String, _
ByVal Level As Long, _
pPrinterEnum As Any, _
ByVal cdBuf As Long, _
pcbNeeded As Long, _
pcReturned As Long) _
As Long

Private Declare Function StrLen Lib "kernel32" _
Alias "lstrlenA" _
(ByVal Ptr As Long) _
As Long

Private Declare Function StrCopy Lib "kernel32" _
Alias "lstrcpyA" _
(ByVal RetVal As String, _
ByVal Ptr As Long) _
As Long
---
Private Function CopyStringFromPtr(ByVal pSource As Long) As String
CopyStringFromPtr = Space$(StrLen(pSource))
StrCopy CopyStringFromPtr, pSource
End Function
---
Public Function GetPrinterNames() As Variant
Dim fSuccess As Boolean, lBuflen As Long, lFlags As Long
Dim aBuffer() As Long, lEntries As Long
Dim iCount As Integer, aPrinters() As String
lFlags = PRINTER_ENUM_LOCAL Or PRINTER_ENUM_CONNECTIONS
Call EnumPrinters(lFlags, vbNullString, 1, 0, 0, lBuflen, lEntries)
ReDim aBuffer(lBuflen \ 4)
fSuccess = EnumPrinters( _
lFlags, _
vbNullString, _
1, _
aBuffer(0), _
lBuflen, _
lBuflen, _
lEntries) <> 0
If fSuccess And lEntries > 0 Then
ReDim aPrinters(lEntries - 1)
For iCount = 0 To lEntries - 1
aPrinters(iCount) = CopyStringFromPtr(aBuffer(iCount * 4 + 2))
Next
GetPrinterNames = aPrinters
End If
End Function
------------------------------------------------------------------------------
-----------
then set this up in it's own module with a call from my form

Public Function GetPrinterList() As String
Dim aPrinters As Variant, i As Integer
aPrinters = GetPrinterNames
If IsArray(aPrinters) Then
For i = 0 To UBound(aPrinters)
Debug.Print aPrinters(i)
Next
End If
GetPrinterList = aPrinters(i)
End Function

I can see the printers in the debug window but it doesn't like my code, it's
giving me a runtime error 9 subscript out of range, I suspect it's something
simple
 
The problem is the last line of the GetPrinterList function,

GetPrinterList = aPrinters(i)

The statement For i = 0 To UBound(aPrinters) leaves i equal to one more than
UBound(aPrinters), so when you get to that last line, i is too big.

What did you want GetPrinterList to return?
 
Thanks for the quick response Douglas,
I would like a list of each printer device, preferably with a line feed at
the end of each device so I can pass the entire string to a field in my table
 
It makes me crazy when the original poster just leaves a final message in a
thread saying never mind, I've resolved it and they don't show the solution.

here it is.

last module should be
Public Function GetPrinterList() As String
Dim strPrinters As String
Dim aPrinters As Variant, i As Integer
aPrinters = GetPrinterNames
If IsArray(aPrinters) Then
For i = 0 To UBound(aPrinters)
strPrinters = strPrinters & aPrinters(i) & vbCrLf
Next
End If
GetPrinterList = strPrinters
End Function

it's almost like I know what I am doing!!

thanks to all who replied.
 
Back
Top