简体   繁体   中英

Excel VBA Set Print Area and Print ALL

I am new to the wonders and world of Macro, VBA and RPA, and would like to study it more. Recently did a short course on RPA.

Just want to share my problem and throw a question out to the Community here.

My Pain Point:
I'm the person printing the Payslips for the Company.

Currently I am opening all 30+ Single Excel File Payslips generated by Excel VBA (not done by me) for my company one by one and setting to print by Set Print Area for EACH Payslip Standalone Excel File, and printing them one by one.

This takes up quite some time which I believe can be saved with either the right Print Settings, VBA or RPA.

Unfortunately I am still exploring these and know nothing about VBA.

Id like to check for VBA, how I can go about macro-ing the process such that I can ease my workflow in the following:

  1. Opening Payslip one by one

  2. Setting the Print Area (same throughout)

  3. Printing

If any of these can be automated it'd save me time and frustration.

Regarding the code it might be a merge of these two

https://www.ablebits.com/office-addins-blog/2019/08/20/set-change-print-area-excel/

https://www.excelhowto.com/macros/print-all-workbooks-in-a-folder/

Anyone can advise step by step what I am to do? I have read and tried but do not understand still.

Tested and working.
This code loops files in a folder, selects A1:K41 and prints the selected range to your standard printer.

Sub run()
    FolderPath = "PATH TO FOLDER"
    FileNme = Dir(FolderPath & "\*.xlsx") ' returns the first xlsx file in the folder
    Do While Len(FileNme) > 0 ' loop while there are files.
        Set wb1 = Workbooks.Open(FolderPath & "\" & FileNme) ' open file

        wb1.Sheets("Sheet1").Range("A1:K41").Select ' select the range

        'below is recorded macro of print selected area
        Application.PrintCommunication = False
        With ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
            .LeftMargin = Application.InchesToPoints(0.7)
            .RightMargin = Application.InchesToPoints(0.7)
            .TopMargin = Application.InchesToPoints(0.75)
            .BottomMargin = Application.InchesToPoints(0.75)
            .HeaderMargin = Application.InchesToPoints(0.3)
            .FooterMargin = Application.InchesToPoints(0.3)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintComments = xlPrintNoComments
            .PrintQuality = 600
            .CenterHorizontally = False
            .CenterVertically = False
            .Orientation = xlPortrait
            .Draft = False
            .PaperSize = xlPaperA4
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            .PrintErrors = xlPrintErrorsDisplayed
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        Application.PrintCommunication = True
        Selection.PrintOut Copies:=1, Collate:=True

        'close payslip
        Application.DisplayAlerts = False 
        wb1.Close 
        Application.DisplayAlerts = True
        FileNme = Dir ' get the next file name
    Loop

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM