gpt4 book ai didi

vba - 在 excel vba 中选择纸张尺寸(非默认尺寸)

转载 作者:行者123 更新时间:2023-12-04 20:06:37 49 4
gpt4 key购买 nike

我有 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。

最佳答案

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

问题2

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

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

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

关于vba - 在 excel vba 中选择纸张尺寸(非默认尺寸),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27939513/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com