Excel Macro Code to select Print Selection.

MarcGib

New Member
Joined
Feb 15, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all, Im very new to using Macros in Excel and was wondering if somebody could help me with coding to achieve automation of my printer function.
1676540125255.png

Thus far I have managed to print the required info to my GX430t label printer, using the above code, however, I have no idea how to auto select in SETTINGS "Print Selection" Only print the current selection"

1676540296214.png

Thank you.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
VBA Code:
Sub PrintTheSelectedArea()
    Selection.PrintOut Copies:=1
End Sub
 
Upvote 0
Hi Frabulator, thank you for the code. When running the code, will that bypass our default Printer setting of "Only print the active sheets" and select "Only print the current selection"?

1676541545678.png


Before our process involved populating the required data, Opening PRINT PREVIEW, selecting ZDesigner GX430t and then selecting "Only print the current selection" and then finaly Printing the label.

1676542212894.png


thanks
 
Upvote 0
will that bypass our default Printer setting of "Only print the active sheets" and select "Only print the current selection"?

The code I posted above will only print the selected using the default settings. You can only have something selected on the active sheet, so yes, it will only print the active sheet and yes it will only print the current selection.

Opening PRINT PREVIEW, selecting ZDesigner GX430t and then selecting "Only print the current selection" and then finaly Printing the label.

All of that can be automated. Please read here.

In my example I include the copies needed. You can include any variable/amount of variables you want or need.

expression.PrintOut (From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName)

Using the code you posted I patched together the following. It should be enough to get you started. I do not know the exact name of your printer; VBA is very specific. You need to have everything correct in the quotations. Capital letters and spaces can sometimes screw you over.

VBA Code:
Sub PrintTheSelectedArea()
    Dim myPrinter As String
    myPrinter = "ZDesigner GX430t"
    'need to select what range you want to print
   
    Selection.PrintOut Copies:=ActiveSheet.Range("B5").Value, collate:=True, ActivePrinter:=myPrinter
   
End Sub

PS:
If you do a quick google search for what you are looking for and include "VBA Excel" at the end of the search query, it will usually bring up some good information :)!
 
Upvote 0
Thank you for all the tips, advise and effort. Really appreciate it. I will get cracking a little later.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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