1004 error on function for printer

J

Janis

I have a printer function to cycle through network numbers 1-9 because the
networks are assigned dynamically. I'm trying to debug it since it stops on
the i = 1 and doesn't loop. I know the current network number is 2.



Public Function getPrinter(ByRef nError As Long)
Dim i As Integer



On Error Resume Next
For i = 1 To 9
Err.Clear
Application.ActivePrinter = "\\martinezfs1-bay\Ca-Martinez-94C on Ne0" &
i & ":"
If Err.Number = 0 Then
getPrinter = i
Else
MsgBox "Network Printer" & " " & i
Exit For

End If
Next
nError = Err.Number <> 0
MsgBox "There was a printer error" & " " & Err.Number
End Function

I thought it might be the scope of the variable but I have a module level
variable for the nError and nPrinter.

Private nNetwork As Integer 'for printer function
Private nError As Long 'forprinter function

It is weird when I step through it. It goes to the else so that means there
isn't an error/ Then I get the printer number 1 in the first msgbox. Then
nError gets set to -1 in the quick watch, then I get another msgBox that the
error is 1004? I can't really tell where the error lies.


----code in sub procedure to call print string-----------
nNetwork = getPrinter(nError)
If nNetwork = 0 Then

MsgBox "the command to print has an error."
MsgBox nError
Else

Application.ActivePrinter = "\\martinezfs1-bay\Ca-Martinez-94C on
Ne0" & nNetwork & ":"
MsgBox nNetwork
End If
 
D

Dave Peterson

You want to return the printer number when err.number = 0.

If Err.Number = 0 Then
getPrinter = i
MsgBox "Network Printer" & " " & i
Exit For
Else
err.clear
End If

This worked ok for me:

Option Explicit
Public Function getPrinter() As Long
Dim i As Long
Dim PtrNumber As Long

PtrNumber = -1
On Error Resume Next
'don't some network printers show up on NE00???
'(I don't recall)
For i = 0 To 9
Application.ActivePrinter _
= "\\martinezfs1-bay\Ca-Martinez-94C on Ne0" & i & ":"
If Err.Number = 0 Then
PtrNumber = i
Exit For
Else
'keep looking
Err.Clear
End If
Next i
On Error GoTo 0

getPrinter = PtrNumber
End Function

Sub aa()
Dim j As Long
j = getPrinter
If j < 0 Then
MsgBox "not found"
Else
MsgBox j
End If
End Sub
 

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

Top