【问题标题】:Excel VBA Set Print Area and Print ALLExcel VBA 设置打印区域并全部打印
【发布时间】:2019-12-10 09:57:48
【问题描述】:

我是 Macro、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/

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

【问题讨论】:

  • 您的问题中缺少很多细节。您在哪个文件夹中打开这些。需要打印的范围/或打印区域。
  • 嗯,它只是我驱动器中的一个文件夹,我不知道如何引导到 VBA 以从网上找到的代码打印大声笑要打印的打印区域来自 A1:K41?

标签: excel vba


【解决方案1】:

经过测试并且可以正常工作。
此代码循环文件夹中的文件,选择 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

【讨论】:

  • 谢谢!我的朋友帮助我快速修复了在工资单模板上设置默认打印区域。然后当我按下宏按钮从主表生成时,它显然遵循工资单模板集打印区域。关于您的解决方案,我也会尝试下一次迭代!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 2017-04-03
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
相关资源
最近更新 更多