在excel vba中选择纸张尺寸(非DEFAULT尺寸)

时间:2015-01-14 09:26:32

标签: vba excel-vba printing excel-2013 page-size

我有兄弟QL-720NW标签打印机,我想打印一些标签。

打印机的卷宽为62毫米

当我尝试打印某些内容时,我需要设置页面,并定义页面大小。 如果页面尺寸不正确(宽度超过62毫米),打印机将无法打印任何内容。

现在我的问题是我正在使用带有宏的excel将一些数据发送到打印机。 我知道有一些预定义的页面大小(http://msdn.microsoft.com/en-us/library/office/ff834612%28v=office.15%29.aspx)可以使用,但在我的情况下,所有这些都太大了。

这是我到目前为止的代码示例:

Sub CreateTestCode()

' setting printer
Dim objPrinter As String
objPrinter = ActivePrinter
ActiveSheet.PageSetup.PrintArea = Range("Img")
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
.PrintHeadings = False
.PrintGridlines = False
.RightMargin = Application.InchesToPoints(0.39)
.LeftMargin = Application.InchesToPoints(0.39)
.TopMargin = Application.InchesToPoints(0.39)
.BottomMargin = Application.InchesToPoints(0.39)
.PaperSize = xlPaperUser
.Orientation = xlLandscape
.Draft = False
End With

Dim printerName As String
printerName = "BrotherQL720NW Labelprinter on XYZ"

ActiveSheet.PrintOut Preview:=True, ActivePrinter:=printerName

ActivePrinter = objPrinter
End Sub

现在我有3个问题:

1:在.PaperSize = xlPaperUser我收到运行时错误' 1004'。无法设置PageSetup类的PaperSize。这有什么不对?

2:如何将纸张尺寸设置为62mm x 50mm?

3:即使我将打印区域定义为Range(" Img"),它仍会打印整张纸??

顺便说一下,我对vba完全不熟悉,这是我第一次尝试使用vba。

2 个答案:

答案 0 :(得分:2)

问题1

xlPaperUser是用户定义的纸张大小,其常量值为256.如果尚未定义,则可能会出错。

问题2

无法在Excel中创建自定义纸张尺寸,但是您可以在许多打印机上创建自定义纸张尺寸。在“页面设置”下,单击“选项”按钮。这将打开打印机属性对话框。使用此对话框将纸张大小更改为自定义大小,然后单击“确定”。

然后在Excel中运行:MsgBox PageSetup.PaperSize。这将为您提供在Excel中分配给该纸张尺寸的新常量值。然后将宏中的.PaperSize = xlPaperUser更改为.PaperSize =&无论你刚刚找到什么常数。

问题3

.PrintArea采用字符串输入,而不是范围。将您的行更改为ActiveSheet.PageSetup.PrintArea = Range("Img").Address,它应该有效。

答案 1 :(得分:0)

我想补充一下。
您还可以添加此行Application.Dialogs(xlDialogPageSetup).Show

一个例子是:

Sub printGraphs()

Application.Dialogs(xlDialogPrinterSetup).Show
Application.Dialogs(xlDialogPageSetup).Show
    With ActiveSheet.PageSetup
        .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)
        .PrintQuality = 1200
        .CenterHorizontally = True
        .CenterVertically = False
        .Orientation = xlLandscape
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintErrors = xlPrintErrorsDisplayed
    End With
        ActiveWorkbook.PrintOut From:=1, To:=3, Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False

End Sub

这会提示用户根据所选打印机选择页面尺寸。我用它来一次打印带有多个选项卡的工作簿,并且只有我想要的选项卡。

相关问题