Printing Problems

F

FCC

Hello,

I have a combo box which automatically fills up with the printers that
are added to the computer's system. And then when the user presses the
print button, I have the following code:


Code:
--------------------

Application.ActivePrinter = printer.Text

--------------------


Where printer.Value is the value of the combo box which has a list of
printers.

But I get the
Run-Time error '1004'
Method 'ActivePrinter' of object '_Application' failed

What is the problem here?
 
N

NickHK

I'm not sure if "Printer" is a reserved word, but it's probably best
avoided.
Sound like the .Text is not a valid setting.
Is it something like "\\MyNetwork\Copier on Ne04:"

NickHK
 
F

FCC

I got rid of the .text but I still got the error:

I made a msg box to see what the printer variable would say and I got a
printer name of:

"EPSON Stylus Photo 915"

Which is a printer on setup on my computer, and the one that I selected
from the combo box.

What is causing this problem!!! :mad:
 
F

FCC

I am using this code that I found on the web:


Code:
--------------------

Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2

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

Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
(ByVal RetVal As String, ByVal Ptr As Long) As Long

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

-----------------
Public Function ListPrinters() As Variant

Dim bSuccess As Boolean
Dim iBufferRequired As Long
Dim iBufferSize As Long
Dim iBuffer() As Long
Dim iEntries As Long
Dim iIndex As Long
Dim strPrinterName As String
Dim iDummy As Long
Dim iDriverBuffer() As Long
Dim StrPrinters() As String

iBufferSize = 3072

ReDim iBuffer((iBufferSize \ 4) - 1) As Long

'EnumPrinters will return a value False if the buffer is not big enough
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, vbNullString, _
1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)

If Not bSuccess Then
If iBufferRequired > iBufferSize Then
iBufferSize = iBufferRequired
Debug.Print "iBuffer too small. Trying again with "; _
iBufferSize & " bytes."
ReDim iBuffer(iBufferSize \ 4) As Long
End If
'Try again with new buffer
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, vbNullString, _
1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
End If

If Not bSuccess Then
'Enumprinters returned False
MsgBox "Error enumerating printers."
Exit Function
Else
'Enumprinters returned True, use found printers to fill the array
ReDim StrPrinters(iEntries - 1)
For iIndex = 0 To iEntries - 1
'Get the printername
strPrinterName = Space$(StrLen(iBuffer(iIndex * 4 + 2)))
iDummy = PtrToStr(strPrinterName, iBuffer(iIndex * 4 + 2))
StrPrinters(iIndex) = strPrinterName
Next iIndex
End If

ListPrinters = StrPrinters

End Function

----------------
Public Function IsBounded(vArray As Variant) As Boolean

'If the variant passed to this function is an array, the function will return True;
'otherwise it will return False
On Error Resume Next
IsBounded = IsNumeric(UBound(vArray))

End Function

----------

--------------------


And this is the code I am using to populate the combo box

Code:
--------------------

Private Sub userForm_Initialize()

Dim StrPrinters As Variant, x As Long

StrPrinters = ListPrinters

'Fist check whether the array is filled with anything, by calling another function, IsBounded.
If IsBounded(StrPrinters) Then
For x = LBound(StrPrinters) To UBound(StrPrinters)
printer.AddItem StrPrinters(x)
Next x
printer.Value = StrPrinters(1)
Else
Debug.Print "No printers found"
End If

End Sub
 
N

NickHK

Yes, the strings from this function do not match the strings returned from
?Application.ActivePrinter
You will see the port info is missing.
It's easier to use
Application.Dialogs(xlDialogPrinterSetup).Show
or
Application.Dialogs(xlDialogPrint).Show

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Descenders clipped 0
macOS Office365 Word 0
Dynamic Naming of Form Button 3
NetWork Printer Selection Problems 1
Application.top=0 fails 3
Run time error 2
Unexpected Error 3
Control printing? 2

Top