Print to printer name listed in table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhughes2187
    New Member
    • Mar 2008
    • 32

    Print to printer name listed in table

    Hey all,
    The access app I am creating, pulls a .dbf file into a table in access. The name of the printer to be printed to is contained in that file/table.

    How would I cycle through the list of printers on my pc until i matched the name in the table and then set the printer to that specific printer..?

    I am trying this code and it doesn't seem to work
    Code:
    prntpdf = DLookup("printerpdf", "scarlet") 'Get printer name to print to
      'Set printer object to specified printer
      x = 0
       Do While Application.Printers(x).DeviceName = prntpdf
        x = x + 1
       Loop
       Set Application.Printer = Application.Printers(x)
  • mshmyob
    Recognized Expert Contributor
    • Jan 2008
    • 903

    #2
    Use the following code to display your printers on your system

    [code=vb]
    Dim prtLoop As Printer

    For Each prtLoop In Application.Pri nters
    With prtLoop
    MsgBox "Device name: " & .DeviceName & vbCr _
    & "Driver name: " & .DriverName & vbCr _
    & "Port: " & .Port
    End With
    Next prtLoop
    [/code]

    You can then reference the printer you want by either the printer name or index within the printer collection (ie: 1st printer is index 0 so it would be Printers(0) and so on.).

    cheers,


    Originally posted by bhughes2187
    Hey all,
    The access app I am creating, pulls a .dbf file into a table in access. The name of the printer to be printed to is contained in that file/table.

    How would I cycle through the list of printers on my pc until i matched the name in the table and then set the printer to that specific printer..?

    I am trying this code and it doesn't seem to work
    Code:
    prntpdf = DLookup("printerpdf", "scarlet") 'Get printer name to print to
      'Set printer object to specified printer
      x = 0
       Do While Application.Printers(x).DeviceName = prntpdf
        x = x + 1
       Loop
       Set Application.Printer = Application.Printers(x)

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by bhughes2187
      Hey all,
      The access app I am creating, pulls a .dbf file into a table in access. The name of the printer to be printed to is contained in that file/table.

      How would I cycle through the list of printers on my pc until i matched the name in the table and then set the printer to that specific printer..?

      I am trying this code and it doesn't seem to work
      Code:
      prntpdf = DLookup("printerpdf", "scarlet") 'Get printer name to print to
        'Set printer object to specified printer
        x = 0
         Do While Application.Printers(x).DeviceName = prntpdf
          x = x + 1
         Loop
         Set Application.Printer = Application.Printers(x)
      You will definately benefit by viewing this prior Tip of the Week:

      Comment

      • bhughes2187
        New Member
        • Mar 2008
        • 32

        #4
        Ok none of the suggestions help me so far.. Let me make it more clear. Depending on what user created the .dbf file containing the printer name, the printer will be different.. (ie. greta, romeo, excalibur)

        These are all printers installed on my printer.

        If I set prntpdf = to one of those names, how would I go about looping through the installed printer names until I matched the value of prntpdf and then set the printer to that index?

        This is to be a totally automated function, the goal is to set a batch of reports to print and print them to a specific printer with out user intervention.

        Comment

        • bhughes2187
          New Member
          • Mar 2008
          • 32

          #5
          Originally posted by mshmyob
          Use the following code to display your printers on your system

          [code=vb]
          Dim prtLoop As Printer

          For Each prtLoop In Application.Pri nters
          With prtLoop
          MsgBox "Device name: " & .DeviceName & vbCr _
          & "Driver name: " & .DriverName & vbCr _
          & "Port: " & .Port
          End With
          Next prtLoop
          [/code]

          You can then reference the printer you want by either the printer name or index within the printer collection (ie: 1st printer is index 0 so it would be Printers(0) and so on.).

          cheers,

          Actually, This helped me out once I had a moment to stop and think about it... Thanks!

          Comment

          • mshmyob
            Recognized Expert Contributor
            • Jan 2008
            • 903

            #6
            You're welcome.

            cheers,

            Originally posted by bhughes2187
            Actually, This helped me out once I had a moment to stop and think about it... Thanks!

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Wouldn't Application.Pri nters(prntpdf) identify the correct printer for you most easily?
              Code:
              prntpdf = DLookup("printerpdf", "scarlet") 'Get printer name to print to
              'Set printer object to specified printer
              Set Application.Printer = Application.Printers(prntpdf)

              Comment

              • bhughes2187
                New Member
                • Mar 2008
                • 32

                #8
                Originally posted by NeoPa
                Wouldn't Application.Pri nters(prntpdf) identify the correct printer for you most easily?
                Code:
                prntpdf = DLookup("printerpdf", "scarlet") 'Get printer name to print to
                'Set printer object to specified printer
                Set Application.Printer = Application.Printers(prntpdf)
                I tried that and it kept erroring out.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  Where? How? Works fine for me in Access 2003 on XP.

                  Which line of the code and what error number / message?

                  Comment

                  • bhughes2187
                    New Member
                    • Mar 2008
                    • 32

                    #10
                    Originally posted by NeoPa
                    Where? How? Works fine for me in Access 2003 on XP.

                    Which line of the code and what error number / message?
                    I would have to get in to the code and change it to tell you. I am heading out on vacay so will be unable to until next week.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      Sounds like a perfectly reasonable excuse to me :D

                      And congratulations on achieving full member status.

                      Comment

                      Working...