help with printer VBA code

Jagat Pavasia

Active Member
Joined
Mar 9, 2015
Messages
359
Office Version
  1. 2021
Platform
  1. Windows
dear sir,

I have 3 printers named in my windows 10 PC:
1) Samsung SCX-3200
2) Brother 7535dw
3) Brother QL-800

my default printer is Samsung SCX-3200, generally I print in this printer.

But now I want to Print my excel workbook sheet in selected area from A11:E19 with "Brother QL-800" the label Printer.

and then after printing my default printer should be returned to originally "Samsung SCX-3200".

I want to add macro in my sheet. (with Print Button)

I have attached screenshot for more details.



thank you .......

please help me
Capture.PNG
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
In a Standard Module:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
    Private Declare PtrSafe Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
#Else
    Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
    Private Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
#End If


Sub PrintToSpecificPrinter( _
    ByVal ObjectToPrint As Object, _
    ByVal PrinterName As String _
)
    Dim sCurrentPrinter As String
   
    sCurrentPrinter = GetActivePrinter
    If SetDefaultPrinter(PrinterName) Then
        ObjectToPrint.PrintOut
        Call SetDefaultPrinter(sCurrentPrinter)
    Else
        MsgBox "Printing failed.", vbCritical
    End If

End Sub

Private Function GetActivePrinter() As String
    Dim sBuffer As String * 128, lBuffSize As Long
    lBuffSize = 128
    If GetDefaultPrinter(sBuffer, lBuffSize) Then
        GetActivePrinter = Left(sBuffer, lBuffSize - 1)
    End If
End Function


Usage of the above code :
VBA Code:
Sub Test()
    PrintToSpecificPrinter ObjectToPrint:=Range("A11:E19"), PrinterName:="Brother QL-800"
End Sub
 
Upvote 0
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
    Private Declare PtrSafe Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
#Else
    Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
    Private Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
#End If


Sub PrintToSpecificPrinter( _
    ByVal ObjectToPrint As Object, _
    ByVal PrinterName As String _
)
    Dim sCurrentPrinter As String
  
    sCurrentPrinter = GetActivePrinter
    If SetDefaultPrinter(PrinterName) Then
        ObjectToPrint.PrintOut
        Call SetDefaultPrinter(sCurrentPrinter)
    Else
        MsgBox "Printing failed.", vbCritical
    End If

End Sub

Private Function GetActivePrinter() As String
    Dim sBuffer As String * 128, lBuffSize As Long
    lBuffSize = 128
    If GetDefaultPrinter(sBuffer, lBuffSize) Then
        GetActivePrinter = Left(sBuffer, lBuffSize - 1)
    End If
End Function


Sub PrintAddress()
    PrintToSpecificPrinter ObjectToPrint:=Range("A11:E19"), PrinterName:="Brother QL-800"
End Sub




I have enter this VBA code in my sheet, and it is working as it should,
but when I single click on macro then it did not print out from brother QL-800
when I click second time on macro then print.

i want print in single click
 
Upvote 0
dear sir,

I have 3 printers named in my windows 10 PC:
1) Samsung SCX-3200
2) Brother 7535dw
3) Brother QL-800

my default printer is Samsung SCX-3200, generally I print in this printer.

But now I want to Print my excel workbook sheet in selected area from A11:E19 with "Brother QL-800" the label Printer.

and then after printing my default printer should be returned to originally "Samsung SCX-3200".

I want to add macro in my sheet. (with Print Button)

I have attached screenshot for more details.



thank you .......

please help meView attachment 80472
I WANT PRINT IN ONLY MY BROTHER QL-800 FROM THIS SHEET
 
Upvote 0
I HAVE 3 PRINTER DRIVER INSTALLED IN MY COMPUTER.

ON OF THEM IS BROTHER LABEL PRINTER.

I WANT TO PRINT ON MY BROTHER PRINTER NAMED "Brother QL-800" IN SELECTED RANGE IS "A11:E19"

SO, I WANT MACRO THAT IF I SELECT MACRO & THEN AUTOMATICALLY SELECT MY BROTHER PRINTER AND INSTANT PRINT FROM "A11:E19".
(Printer should be automatically select "Brother QL-800" & then print)



PLEASE GIVE ME VBA CODE, I AM NEW IN VBA.
 
Upvote 0
Hi Jagat,

I have already given you the code. I don't know why the code doesn't work for you upon the first click on the PrintAddress macro.

Try debugging the code by stepping through pressing the F8 key and see if you can figrue out where the problem lies.
 
Upvote 0
Hi Jagat,

I have already given you the code. I don't know why the code doesn't work for you upon the first click on the PrintAddress macro.

Try debugging the code by stepping through pressing the F8 key and see if you can figrue out where the problem lies.
dear jaafar Tribak,

It is working but not as I want.

for example : My current default printer is selected to "Samsung SCX-3200". (In Printer Management in setting of Printer Properties)
and now I print from clicking PrintAddress macro then did not print on my "Brother QL-800",
The print come out from Samsung Printer, I want Print from Brother Ql-800.

I want only that if I click macro on PrintAddress then only print from Brother QL-800, even my default printer selection is whatever.

Please help me

waiting for your answer.

and sorry for my argument..
 
Upvote 0
Hi Jagat, I understand what you want and I also understand the problem you are having.

The ode I gave you works for me just fine . I couldn't repoduce the issue you are describing. That's why I asked you to try to debug the code and see if you can figure out why it is not working for you.

What happens when you run the following :
VBA Code:
Call SetDefaultPrinter("Brother QL-800")

Does that change the default printer for you ?
 
Upvote 0
Hi Jagat, I understand what you want and I also understand the problem you are having.

The ode I gave you works for me just fine . I couldn't repoduce the issue you are describing. That's why I asked you to try to debug the code and see if you can figure out why it is not working for you.

What happens when you run the following :
VBA Code:
Call SetDefaultPrinter("Brother QL-800")

Does that change the default printer for you ?
Screenshot 2022-12-10 111230.png





i found this error when i apply
Screenshot 2022-12-10 111613.png
 
Upvote 0
I want something like

Sub PrintAddress()
Set mynetwork = CreateObject("WScript.network")
mynetwork.SetDefaultPrinter "Brother QL-800"
Sheets("Sheet7").Range("A11:E19").PrintOut
mynetwork.SetDefaultPrinter "Samsung SCX-3200 Series"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top