繁体   English   中英

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

[英]Excel VBA Set Print Area and Print ALL

我对宏、VBA 和 RPA 的奇迹和世界不熟悉,并想进一步研究它。 最近做了一个关于 RPA 的短期课程。

只是想在这里分享我的问题并向社区提出问题。

我的痛点:
我是为公司打印工资单的人。

目前,我正在为我的公司一一打开由 Excel VBA 生成的所有 30 多个单个 Excel 文件工资单(不是由我完成的),并设置为通过“为每个工资单独立 Excel 文件设置打印区域”进行打印,并一一打印。

这需要相当长的时间,我相信可以通过正确的打印设置、VBA 或 RPA 来节省。

不幸的是,我仍在探索这些并且对 VBA 一无所知。

我想检查 VBA,我如何对过程进行宏处理,以便我可以在以下方面简化我的工作流程:

  1. 一张一张开工资单

  2. 设置打印区域(始终相同)

  3. 印刷

如果这些中的任何一个可以自动化,那将节省我的时间和挫折。

关于代码,它可能是这两个的合并

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/

任何人都可以逐步建议我该怎么做? 我已经阅读并尝试过,但仍然不明白。

测试和工作。
此代码循环文件夹中的文件,选择 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