PC Review


Reply
Thread Tools Rate Thread

1004 error on function for printer

 
 
=?Utf-8?B?SmFuaXM=?=
Guest
Posts: n/a
 
      23rd Aug 2007
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
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      23rd Aug 2007
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 With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
run time error 1004 general odbc error excel 2003 vba =?Utf-8?B?TWVudG9z?= Microsoft Excel Programming 5 24th Jan 2011 02:56 PM
Error 1004 opening workbook 1004 WembleyBear Microsoft Excel Programming 2 30th Nov 2009 01:33 PM
RunTime Error 1004 when setting a series' error bars in Excel 2007 Steve Tummey Microsoft Excel Crashes 0 6th Feb 2008 01:37 PM
Interior.Color from a function yields Error 1004 Diaren Whan Microsoft Excel Programming 3 23rd Jan 2008 12:45 PM
Error 1004 : function returning a Worksheet Michel S. Microsoft Excel Programming 2 19th Jan 2007 02:20 PM


Features
 

Advertising
 

Newsgroups
 


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