Advertisement
burssen

VBA snippets for printing

May 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub SwitchPrinter()
  2.     Dim prt As Printer
  3.     ' Get current default printer
  4.    Set prt = Application.Printer
  5.     ' Set default printer
  6.    Application.Printer = Application.Printers("OtherPrinter")
  7.     ' Print something, e.g.
  8.    DoCmd.PrintOut
  9.     ' Restore original printer
  10.    Set Application.Printer = prt
  11. End Sub
  12.  
  13. Sub ListPrinters()
  14.     Dim prt As Printer
  15.     For Each prt In Printers
  16.         Debug.Print prt.DeviceName
  17.     Next prt
  18. End Sub
  19.  
  20. //http://www.exceltip.com/printing-in-vba/change-the-default-printer-using-vba-in-microsoft-excel.html
  21. Option Explicit
  22.  
  23. Sub PrintToAnotherPrinter()
  24. Dim STDprinter As String
  25.  
  26. STDprinter = Application.ActivePrinter
  27. Application.ActivePrinter = “Microsoft fax on fax:”
  28. Activesheet.Printout
  29. Application.ActivePrinter = STDprinter
  30.  
  31. End Sub
  32.  
  33. Sub printersetup()
  34. curPrinter = Application.ActivePrinter
  35. Application.ActivePrinter = "Microsoft XPS Document Writer on Ne01:"
  36.  
  37.  
  38. ''Print documents
  39.  
  40. Application.ActivePrinter = curPrinter
  41. End Sub
  42.  
  43. Sub FindPrinter()
  44. Dim strPName As String
  45. strPName = Application.ActivePrinter
  46. '******Finds the current printer name ********
  47.  
  48. Application.ActivePrinter = "Nitro PDF Creator 2 on Ne08:"
  49. ***** Does your print job *******
  50.  
  51. Application.ActivePrinter=strPName
  52. **** Then resets the printer to it's default*****
  53. End Sub
  54.  
  55. ''https://www.mrexcel.com/forum/excel-questions/631263-set-default-printer.html
  56. Function GetPrinterWithPort(ByVal PrinterName As String) As String
  57.  
  58.     Dim Port As Variant
  59.     Dim WSH As Object
  60.    
  61.         Set WSH = CreateObject("WScript.Shell")
  62.             On Error Resume Next
  63.             Port = WSH.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\" & PrinterName)
  64.             If Err = 0 Then
  65.                 Port = Split(Port, ",")(1)
  66.                 GetPrinterWithPort = PrinterName & " on " & Port
  67.             End If
  68.             On Error GoTo 0
  69.    
  70. End Function
  71.  
  72. Sub PrintPersonalPdf()
  73.     Dim strPName As String
  74.         strPName = Application.ActivePrinter
  75.         Application.ActivePrinter = GetPrinterWithPort("Nitro PDF Creator 2")
  76.         Application.Run "PrintPersonalFax"
  77.         Application.ActivePrinter = strPName
  78. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement