簡體   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