VBA打印宏以特定方式设置页面

时间:2016-09-16 20:55:46

标签: excel vba excel-vba printing

我一直在研究一个我觉得很简单的打印宏。我已经尝试过录制一个宏,并且已经研究了几个小时来查看其他人的代码。我想要的是宏:

1)选择活动工作表中的所有单元格

2)设置打印比例以使所有列适合一页

3)打印横向模式

4)打开打印预览(如果可能)

5)如果无法进行#4,则执行打印作业。

当我运行当前代码时,我的excel工作表被分成了大量的页面(棋盘格式),然后我得到一个错误代码。谢谢阅读。

这是我目前的代码:

Sub PrintNOPAsheet()'
' PrintNOPAsheet Macro



 Cells.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$A$1:$H$346"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.25)
    .RightMargin = Application.InchesToPoints(0.25)
    .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 = xlLandscape
    .Draft = False
    .PaperSize = xlPaperLetter
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = 100
    .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, IgnorePrintAreas:=False
End Sub

'

1 个答案:

答案 0 :(得分:1)

以下是我通常使用的内容,然后我将其与您的问题相匹配。在http://localhost:8080/test/public/api/test中,您可以从录制的宏中添加尽可能多的属性以适合您的代码。

With