Excel VBA 设置打印区域并全部打印



我对宏、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

最新更新