469,963 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Print to Specific Printer

I have a button that prints a report and runs a query to update the records whose invoice was printer.

I have the report setup to print on a specific printer.

It's been working fine for months, then all of a sudden yesterday it printed on our mono machine instead of our colour one. Which meant I had to go into the queries and reset the ones that had been updated.
When I opened the report in design view and it said the specific printer was unavailable, so I reselected it (exactly the same IP, name, everything), saved and it ran fine.

This morning, same thing happened again!

I can't have this happening every day so I'm wondering if there's any VBA I can add to the onPage event to make sure it selects the right printer?


Thanks
Mandi

Edit:
I just found this code:
Expand|Select|Wrap|Line Numbers
  1. reports(0).Printer.devicename = "My Printer" 
But it would be easier if I could specify it using the IP address instead of the name, as there's 20+ computers and I can't be sure that everyone has the same printer name setup, but obviously the IP doesn't change.
Mar 26 '09 #1
10 9923
NeoPa
32,233 Expert Mod 16PB
Printer objects have a Port property where an IP address, if used specifically, should appear. Not all printers use IP addressing ports, and not all that do use the IP address in its native form (mine for instance always refer to the name of the device although they are ports that address it via IP).

Assuming for now though, that your ports indicate the IP address somehow though, there is also a collection of Printers available. If you were to cycle through this collection looking for a printer whose port matched what you were after, then assign this printer to your report, then that should work for you.
Expand|Select|Wrap|Line Numbers
  1. Set Reports("Your report name").Printer = _
  2.         Printers("Found Printer Name")
Mar 26 '09 #2
Ah.
Each computer in the company has a random number of installed printers all attached via IP, mine for instance currently has 21. Most of the other computers were set up before I got here (which is why I don't know who has which, what each one is called etc.)

In the Printer properties, the Port Name is IP_xx.xx.xx.xx (obviously xx is the specific IP address), I'm assuming that's the part you mean?
Mar 26 '09 #3
NeoPa
32,233 Expert Mod 16PB
@mandanarchi
It is, but what is the name of your report?
Mar 26 '09 #4
NeoPa
32,233 Expert Mod 16PB
Try working with this basic code. The name of the report and the IP address (in standard x.x.x.x format) are the two parameters required :
Expand|Select|Wrap|Line Numbers
  1. Public Sub UsePrinterIP(strReport As string, ByVal strIP As String)
  2.   Dim ptrThis As Printer
  3.  
  4.   strIP = "IP_" & strIP
  5.   For Each ptrThis In Printers
  6.     If ptrThis.Port = strIP Then Exit For
  7.   Next ptrThis
  8.   If ptrThis.Port = strIP Then
  9.     Set Reports(strReport).Printer = ptrThis
  10.   Else
  11.     Call MsgBox("...not worked...")
  12.   End If
  13. End Sub
Mar 26 '09 #5
Run-time error '91':
Object variable or With block variable not set

Error on this line
Expand|Select|Wrap|Line Numbers
  1. If ptrThis.Port = strIP Then
Mar 26 '09 #6
NeoPa
32,233 Expert Mod 16PB
That means the ptrThis object variable is unset. There was no printer found to match the criteria. This is the part that you pick up with and progress yourself if you can.

If not then I will need a reasonable explanation why before simply doing it for you. This is not intended to be supplying a ready-made solution, just to get you past the problem you started out with.
Mar 26 '09 #7
ChipR
1,287 Expert 1GB
Isn't that variable unset because it's ouside the scope of the For loop?
Mar 26 '09 #8
NeoPa
32,233 Expert Mod 16PB
No Chip. It's unset (or set to the value Nothing) because it didn't find any matching Printer object within the For loop.

For loops can often be used for finding a value and then using it (referring to it) when the loop has completed. In this case, we immediately detect a find and break out of the loop for this purpose.
Mar 26 '09 #9
I wasn't asking you to do it for me NeoPa, I was asking for help.
I didn't understand what the error message was saying so I asked, that's all.

I think I more or less understand it now, and Google is my friend, so thanks for all your help.

Mandi.
Mar 27 '09 #10
NeoPa
32,233 Expert Mod 16PB
Sorry if I sounded accusatory Mandi. That wasn't really my intention. I was simply trying to explain why I wasn't going further at this time.

Different people have different understandings and many it seems, need that explaining. I wasn't to know you understood it better and didn't need it ;)
Mar 31 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Peter Lin | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.