Print to Specific Printer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mandanarchi
    New Member
    • Sep 2008
    • 90

    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:
    Code:
    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.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    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.
    Code:
    Set Reports("Your report name").Printer = _
            Printers("Found Printer Name")

    Comment

    • mandanarchi
      New Member
      • Sep 2008
      • 90

      #3
      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?

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        Originally posted by mandanarchi
        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?
        It is, but what is the name of your report?

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          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 :
          Code:
          Public Sub UsePrinterIP(strReport As string, ByVal strIP As String)
            Dim ptrThis As Printer
          
            strIP = "IP_" & strIP
            For Each ptrThis In Printers
              If ptrThis.Port = strIP Then Exit For
            Next ptrThis
            If ptrThis.Port = strIP Then
              Set Reports(strReport).Printer = ptrThis
            Else
              Call MsgBox("...not worked...")
            End If
          End Sub

          Comment

          • mandanarchi
            New Member
            • Sep 2008
            • 90

            #6
            Run-time error '91':
            Object variable or With block variable not set

            Error on this line
            Code:
            If ptrThis.Port = strIP Then

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              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.

              Comment

              • ChipR
                Recognized Expert Top Contributor
                • Jul 2008
                • 1289

                #8
                Isn't that variable unset because it's ouside the scope of the For loop?

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  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.

                  Comment

                  • mandanarchi
                    New Member
                    • Sep 2008
                    • 90

                    #10
                    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.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      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 ;)

                      Comment

                      Working...