+ Reply to Thread
Results 1 to 5 of 5

how to call for printer function

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-24-2012
    Location
    San Diego, USA
    MS-Off Ver
    Excel 2019
    Posts
    870

    how to call for printer function

    I have a printer, and finally figured out that it changes on the name, so its named like this:

    Canon on NE01:
    Canon on NE02:
    Canon on NE04:
    Canon on NE05:
    and so forth
    after searching it came up to something that works on dynamic names of printers from this link: https://www.excelforum.com/excel-pro...numbering.html

    If someone that can help me write the sub to print on whatever the printer name is to print the below sheets:
    Sheet4.PrintOut
    i dont know how to call for functions, and just started reading about functions, unsure how to use it yet. If someone would kindly moc up a simple example of this please
    Arkadi in post #30 on that link, if someone that can merged it to print out sheet4 please
    Last edited by chubbychub; 01-16-2024 at 01:51 AM.

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,085

    Re: how to call for printer function

    That function will work
    Put it in a standard module.
    'Written: November 28, 2009
    'Author:  Leith Ross
    'Summary: Finds a printer by name and returns the printer name and port number.
    
    Function FindPrinter(ByVal PrinterName As String) As String
    
     'This works with Windows 2000 and up
     
      Dim Arr As Variant
      Dim Device As Variant
      Dim Devices As Variant
      Dim Printer As String
      Dim RegObj As Object
      Dim RegValue As String
      Const HKEY_CURRENT_USER = &H80000001
           
        Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
        RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
        
          For Each Device In Devices
            RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
            Printer = Device & " on " & Split(RegValue, ",")(1)
            If InStr(1, Printer, PrinterName, vbTextCompare) > 0 Then
               FindPrinter = Printer
               Exit Function
            End If
          Next
          
          
    End Function

    Then call it from your print routine, e.g.
    I've highlighted the function line. Replace "Microsoft Print to PDF" with the full name of your printer as displayed in file/print
    Sub ToPrint()
    Dim CurrPrinter As String
    Dim SetPrinter As String
    
    CurrPrinter = ActivePrinter 'remember the current printer
    
    SetPrinter = FindPrinter("Microsoft Print to PDF")  'call the function on the printer
    If Len(SetPrinter) Then
        ActivePrinter = SetPrinter 'if no error, change the active printer
        Sheet4.PrintOut 'perform the print
        ActivePrinter = CurrPrinter 'change the active printer back to what it was before
    End If
    End Sub
    Last edited by ByteMarks; 01-16-2024 at 07:05 AM.

  3. #3
    Forum Contributor
    Join Date
    10-24-2012
    Location
    San Diego, USA
    MS-Off Ver
    Excel 2019
    Posts
    870

    Re: how to call for printer function

    thank you Bytemarks for the reply, its sending to the wrong printer, the Printer name changes depeding on which computer, the name of the printer is "Canon on NE01:" "Canon on NE02:", "Canon on NE03" and so forth, depeding on the computer that the macro runs, so can the code in that link from arkadi post #30 be linked to Leith Ross code can be call to search for the printer name and call for it?
    i want tocall for this function before it prints :
    'by arkadi in post #30 on the link posted
    Public Function prnfull(prnname As String) As String
    
    Dim prnport, strsetting As String
    strsetting = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices\" & prnname)
    prnport = Split(strsetting, ",")
    prnfull = prnname & " on " & prnport(1)
    
    End Function
    Last edited by chubbychub; 01-16-2024 at 08:51 AM.

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,085

    Re: how to call for printer function

    The on NE01: part is port and is provided by the function which retrieves it from the registry
    So if the name of the printer is "Canon" you could try

    Sub ToPrint()
    Dim CurrPrinter As String
    Dim SetPrinter As String
    
    CurrPrinter = ActivePrinter 'remember the current printer
    
    SetPrinter = FindPrinter("Canon")  'call the function on the printer
    If Len(SetPrinter) Then
        ActivePrinter = SetPrinter 'if no error, change the active printer
        Sheet4.PrintOut 'perform the print
        ActivePrinter = CurrPrinter 'change the active printer back to what it was before
    End If
    End Sub

  5. #5
    Forum Contributor
    Join Date
    10-24-2012
    Location
    San Diego, USA
    MS-Off Ver
    Excel 2019
    Posts
    870

    Re: how to call for printer function

    Bytemarks! thank you! i was trying to figure out how your code works and how it was calling for port number NE0x.. but your code but it loops thru all the printers and just matches what its call from the computer which is great! its better than what i was expecting!. you the best and thank you again! no one answered or replied but you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can VBA send information to barcode Zebra printer and command it to printer
    By samihaddad2014 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2020, 04:11 PM
  2. Replies: 1
    Last Post: 06-30-2017, 10:29 AM
  3. [SOLVED] Solution to changing Default printer code when printer changes
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-28-2016, 03:12 PM
  4. [SOLVED] ExecuteExcel4Macro, active printer - prints to another printer - worked on 2007!
    By wonderdunder in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-29-2015, 10:37 AM
  5. Replies: 0
    Last Post: 11-18-2013, 03:43 AM
  6. Registering Printer Library on computer without printer installed (VBA)
    By Haydan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2013, 03:30 AM
  7. macro to print 2 copies to default printer, then 1 copy to Adobe PDF printer
    By jwright650 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2011, 01:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1