简体   繁体   English

Excel VBA 设置打印区域并打印所有

[英]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.我对宏、VBA 和 RPA 的奇迹和世界不熟悉,并想进一步研究它。 Recently did a short course on RPA.最近做了一个关于 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.目前,我正在为我的公司一一打开由 Excel VBA 生成的所有 30 多个单个 Excel 文件工资单(不是由我完成的),并设置为通过“为每个工资单独立 Excel 文件设置打印区域”进行打印,并一一打印。

This takes up quite some time which I believe can be saved with either the right Print Settings, VBA or RPA.这需要相当长的时间,我相信可以通过正确的打印设置、VBA 或 RPA 来节省。

Unfortunately I am still exploring these and know nothing about VBA.不幸的是,我仍在探索这些并且对 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:我想检查 VBA,我如何对过程进行宏处理,以便我可以在以下方面简化我的工作流程:

  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.ablebits.com/office-addins-blog/2019/08/20/set-change-print-area-excel/

https://www.excelhowto.com/macros/print-all-workbooks-in-a-folder/ 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.此代码循环文件夹中的文件,选择 A1:K41 并将所选范围打印到标准打印机。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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