Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default 1004 error on function for printer

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 1004 error on function for printer

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

Janis wrote:

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


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
run time error 1004 general odbc error excel 2003 vba Mentos Excel Programming 5 January 24th 11 02:56 PM
Error 1004 : function returning a Worksheet Michel S. Excel Programming 2 January 19th 07 02:20 PM
run-time error '1004': Application-defined or object-deifined error [email protected] Excel Programming 5 August 10th 05 09:39 PM
Excel 2003 Macro Error - Runtime error 1004 Cow Excel Discussion (Misc queries) 2 June 7th 05 01:40 PM


All times are GMT +1. The time now is 12:51 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"