我试图通过以下方式设置自定义纸张尺寸:
Printer.Height = 2160
Printer.Width = 11900
但它没有任何影响。设置完毕后,我会询问这些值并返回默认值。这个:
Printer.PaperSize = 256
返回错误...
任何想法??
答案 0 :(得分:3)
您的打印机不允许设置这些属性,或者您超出了其允许的最大值。来自Visual Basic Reference
如果设置高度和宽度 打印机驱动程序的属性 不允许这些属性 设置,没有错误发生和大小 这篇论文保持不变。如果你 设置打印机的高度和宽度 只允许某些值的驱动程序 要指定,不会发生错误 该属性设置为任何 司机允许。例如,你可以 设置高度为150,司机会 将它设置为144。
我不知道为什么在将Papersize属性设置为256时出现错误。它适用于我。另外,the documentation表示“设置打印机的高度或宽度属性会自动将PaperSize设置为vbPRPSUser。”,等于256。
答案 1 :(得分:2)
我实际上遇到了同样的问题,但我碰巧找到了一个突破。 首先,您需要创建一个定义自定义纸张尺寸的自定义表单。然后,你需要 请参阅Windows API以检查您刚刚创建的表单名称。你会得到名字 从函数返回的数组,并使用找到表单名称的数组索引。 最后将它用作printer.papersize
的值以下示例:
Public Type PRINTER_DEFAULTS
pDatatype As Long
pDevMode As Long
DesiredAccess As Long
End Type
Public Type FORM_INFO_1
Flags As Long
pName As Long ' String
Size As SIZEL
ImageableArea As RECTL
End Type
Public Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _
(ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _
ByVal cbBuf As Long, ByRef pcbNeeded As Long, _
ByRef pcReturned As Long) As Long
Public Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
(pDest As Any, pSource As Any, ByVal cbLength As Long)
Public Declare Sub Sleep Lib "KERNEL32" (ByVal dwMilliseconds As Long)
Public Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Public Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" _
(ByVal lpString1 As String, ByRef lpString2 As Long) As Long
'UDF
Public Function PtrCtoVbString(ByVal Add As Long) As String
Dim sTemp As String * 512, x As Long
x = lstrcpy(sTemp, ByVal Add)
If (InStr(1, sTemp, Chr(0)) = 0) Then
PtrCtoVbString = ""
Else
PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
End If
End Function
Public Function IsFormExist(ByVal DeviceName As String, ByVal isFormName As String, ByVal PrinterHandle As Long) As Long
Dim NumForms As Long, i As Long
Dim FI1 As FORM_INFO_1
Dim pd As PRINTER_DEFAULTS
Dim aFI1() As FORM_INFO_1 ' Working FI1 array
Dim Temp() As Byte ' Temp FI1 array
Dim FormIndex As Integer
Dim BytesNeeded As Long
Dim RetVal As Long
On Error GoTo cleanup
FormIndex = 0
ReDim aFI1(1)
' First call retrieves the BytesNeeded.
RetVal = OpenPrinter(DeviceName, PrinterHandle, pd)
If (RetVal = 0) Or (PrinterHandle = 0) Then
'Can't access current printer. Bail out doing nothing
Exit Function
End If
RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&, BytesNeeded, NumForms)
ReDim Temp(BytesNeeded)
ReDim aFI1(BytesNeeded / Len(FI1))
' Second call actually enumerates the supported forms.
RetVal = EnumForms(PrinterHandle, 1, Temp(0), BytesNeeded, BytesNeeded, _
NumForms)
Call CopyMemory(aFI1(0), Temp(0), BytesNeeded)
For i = 0 To NumForms - 1
With aFI1(i)
If isFormName = PtrCtoVbString(.pName) Then
' Found the desired form
FormIndex = i + 1
Exit For
End If
End With
Next i
IsFormExist = FormIndex ' Returns the number when form is found.
cleanup:
'Release the printer handle
If (PrinterHandle <> 0) Then Call ClosePrinter(PrinterHandle)
End Function
'Here We Go
dim papercode as long, printername as string, formname as string
printername=printer.Devicename
formname = "myform"
papercode=IsFormExist(printername, formname, Printer.hdc)
if papercode<>0 then
printer.papersize=papercode
end if
试一试,祝你好运
答案 2 :(得分:1)
您确定错误与打印机本身的最大打印宽度无关吗?许多打印机的最大打印宽度为8.25英寸(11880),允许8.5英寸宽纸张两侧的1/4英寸边距。
最快的检查方法是将打印宽度设置为11880或更低,看它是否有效。
另一种可能性是打印机的权限。如果它是共享网络资源,则可能会被锁定。
答案 3 :(得分:0)
解决方案是使用Windows 98.它不适用于win2k,也不适用于winXP。相同的代码,相同的打印机。
问候。
答案 4 :(得分:0)
我正在测试此代码,但我无法在控制面板Windows XP Professional SP3中看到我使用打印机和扫描仪创建的自定义表单。 注意:我可以在regedit中检查此表单是否存在,并且其ID在字符串值中为512,并且它包含在打印机控制面板中创建的表单的名称。
为什么此功能不会返回我的自定义表单,我使用的是HP Laserjet 1020。