2

我有 Brother QL-720NW 标签打印机,我想在上面打印一些标签。

打印机的卷筒宽度为 62mm

当我尝试对其进行打印时,我需要设置页面并定义页面大小。如果页面尺寸不正确(宽度超过 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。

4

2 回答 2

3

问题 1

xlPaperUser是用户定义的纸张尺寸,它被分配了一个常数值 256。如果尚未定义,则可能会引发错误。

问题2

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

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

问题 3

.PrintArea接受字符串输入,而不是范围。将您的线路更改为ActiveSheet.PageSetup.PrintArea = Range("Img").Address,它应该可以工作。

于 2015-01-14T15:58:46.647 回答
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

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

于 2019-10-30T13:47:40.093 回答