vba MS Access:自定义纸张尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11335523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:44:26  来源:igfitidea点击:

MS Access: custom paper size

ms-accessvbaprintingpage-size

提问by CertifiedKennedy

I am building a MS Access (2010) application and I'll be printing barcode labels from this application. There are a variety of other reports and forms which will also be printed, using the standard printer settings, however with the barcodes I need it to print to a specific printer, and have to set it to use a special page size.

我正在构建一个 MS Access (2010) 应用程序,我将从此应用程序打印条形码标签。还有各种其他报告和表格也将使用标准打印机设置进行打印,但是对于条形码,我需要将其打印到特定打印机,并且必须将其设置为使用特殊的页面大小。

In my searching I have found the printer object's property"Papersize" which itself has quite a few 'standard' default options including acPRPSLetter for the standard US Letter at 8.5" x 11" and acPRPSA4 for the A4 paper size. None of the preset sizes will work for my use. There is one preset which represents a user custom size, acPRPSUser, but I have not found any way to programmatically setthe custom size.

在我的搜索中,我发现打印机对象的属性“Papersize”本身有很多“标准”默认选项,包括用于 8.5" x 11" 的标准美国信函的 acPRPSLetter 和用于 A4 纸张尺寸的 acPRPSA4。没有任何预设尺寸适合我使用。有一个预设代表用户自定义大小,acPRPSUser,但我还没有找到任何以编程方式设置自定义大小的方法。

I did read about how there are the '.height' and '.width' properties of the printer, but it does not appear that they exist in the VB used for Access 2010 (I believe it is based on VB6).

我确实读过打印机的“.height”和“.width”属性,但它们似乎不存在于用于 Access 2010 的 VB 中(我相信它基于 VB6)。

Can anyone help me set a custom paper size using VB code in Access 2010?

谁能帮我在 Access 2010 中使用 VB 代码设置自定义纸张尺寸?

回答by NGLN

There is no need for VBA. You can set all page settings for margins, orientation, paper, printer and columns by using the page settings command from the menu: Tools for report designs > Page settings > Page settings > Page > Paper > Size, or > Page > Printer for ReportName > Use specific printer > Printer > Properties. These settings are saved for each induvidual report.

不需要VBA。您可以使用菜单中的页面设置命令设置页边距、方向、纸张、打印机和列的所有页面设置:报表设计工具 > 页面设置 > 页面设置 > 页面 > 纸张 > 大小,或> 页面 >报表名称打印机> 使用特定打印机 > 打印机 > 属性。这些设置会为每个单独的报告保存。

Screenshot

截屏

回答by Kees Dapperens

I had the same problem. I solved it by using How to: Programmatically Retrieve Printer Capabilities

我有同样的问题。我使用How to: Programmatically Retrieve Printer Capabilities解决了这个问题

I made a module with a Procedure Printout. With the Function Printerselection I could call a printer using a specific part of the printername. The function PaperSelection was used to specify the paper using a specific part of the paper name.

我制作了一个带有过程打印输出的模块。使用功能打印机选择,我可以使用打印机名称的特定部分调用打印机。PaperSelection 函数用于使用纸张名称的特定部分指定纸张。

First I had to use the declaration for the DeviceCapabilities function API call

首先,我必须使用 DeviceCapabilities 函数 API 调用的声明

    ' Declaration for the DeviceCapabilities function API call.
Private Declare Function DeviceCapabilities Lib "winspool.drv" _
    Alias "DeviceCapabilitiesA" (ByVal lpsDeviceName As String, _
    ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _
    ByVal lpDevMode As Long) As Long

' DeviceCapabilities function constants.
Private Const DC_PAPERNAMES = 16
Private Const DC_PAPERS = 2
Private Const DC_BINNAMES = 12
Private Const DC_BINS = 6
Private Const DEFAULT_VALUES = 0

Private Type str_DEVMODE
    RGB As String * 94
End Type

Private Type type_DEVMODE
    strDeviceName As String * 32
    intSpecVersion As Integer
    intDriverVersion As Integer
    intSize As Integer
    intDriverExtra As Integer
    lngFields As Long
    intOrientation As Integer
    intPaperSize As Integer
    intPaperLength As Integer
    intPaperWidth As Integer
    intScale As Integer
    intCopies As Integer
    intDefaultSource As Integer
    intPrintQuality As Integer
    intColor As Integer
    intDuplex As Integer
    intResolution As Integer
    intTTOption As Integer
    intCollate As Integer
    strFormName As String * 32
    lngPad As Long
    lngBits As Long
    lngPW As Long
    lngPH As Long
    lngDFI As Long
    lngDFr As Long
End Type

Private Cnt As Integer, PrinterSelect As Integer

Public Sub PrintOut(ByVal rptName As String, Printer As String, Paper As String, BinName As String, Optional Landscape As Boolean, Optional WhereCond)
Dim rpt As Report
DoCmd.OpenReport rptName, acViewPreview, , WhereCond
Set rpt = Reports(rptName)
PrinterSelect = PrinterSelection(Printer)
rpt.Printer = Application.Printers(PrinterSelect)
rpt.Printer.PaperSize = PaperSelection(Paper, PrinterSelect)
If Landscape Then
    rpt.Printer.Orientation = acPRORLandscape
Else
    rpt.Printer.Orientation = acPRORPortrait
End If
rpt.Printer.PaperBin = BinSelection(BinName, PrinterSelect)
End Sub

Public Function PrinterSelection(Printer As String) As Integer
For Cnt = 0 To Application.Printers.Count - 1
    If InStr(1, Application.Printers(Cnt).DeviceName, Printer) > 0 Then
        PrinterSelection = Cnt
    End If
Next Cnt
End Function

Public Function PaperSelection(Paper As String, Printer As Integer) As Integer

    Dim lngPaperCount As Long
    Dim lngCounter As Long
    Dim hPrinter As Long
    Dim strDeviceName As String
    Dim strDevicePort As String
    Dim strPaperNamesList As String
    Dim strPaperName As String
    Dim intLength As Integer
    Dim strMsg As String
    Dim aintNumPaper() As Integer

    On Error GoTo GetPaperList_Err

    ' Get the name and port of the selected printer.
    strDeviceName = Application.Printers(Printer).DeviceName
    strDevicePort = Application.Printers(Printer).Port

    ' Get the count of paper names supported by the printer.
    lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
        lpPort:=strDevicePort, _
        iIndex:=DC_PAPERNAMES, _
        lpOutput:=ByVal vbNullString, _
        lpDevMode:=DEFAULT_VALUES)

    ' Re-dimension the array to the count of paper names.
    ReDim aintNumPaper(1 To lngPaperCount)

    ' Pad the variable to accept 64 bytes for each paper name.
    strPaperNamesList = String(64 * lngPaperCount, 0)

    ' Get the string buffer of all paper names supported by the printer.
    lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
        lpPort:=strDevicePort, _
        iIndex:=DC_PAPERNAMES, _
        lpOutput:=ByVal strPaperNamesList, _
        lpDevMode:=DEFAULT_VALUES)

    ' Get the array of all paper numbers supported by the printer.
    lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
        lpPort:=strDevicePort, _
        iIndex:=DC_PAPERS, _
        lpOutput:=aintNumPaper(1), _
        lpDevMode:=DEFAULT_VALUES)

    ' List the available paper names.
    For lngCounter = 1 To lngPaperCount

        ' Parse a paper name from the string buffer.
        strPaperName = Mid(String:=strPaperNamesList, Start:=64 * (lngCounter - 1) + 1, Length:=64)
        intLength = VBA.InStr(Start:=1, String1:=strPaperName, String2:=Chr(0)) - 1
        strPaperName = Left(String:=strPaperName, Length:=intLength)
        If InStr(1, strPaperName, Paper) > 0 Then
        ' Select the a paper number corresponding to the paper name.
            PaperSelection = aintNumPaper(lngCounter)
        End If
    Next lngCounter


GetPaperList_End:
    Exit Function

GetPaperList_Err:
    MsgBox Prompt:=err.Description, Buttons:=vbCritical & vbOKOnly, _
        Title:="Error Number " & err.Number & " Occurred"
    Resume GetPaperList_End

End Function

Public Function BinSelection(BIN As String, Printer As Integer) As Integer
' Uses the DeviceCapabilities API function to choose the desired paper bin supported by the    chosen printer

    Dim lngBinCount As Long
    Dim lngCounter As Long
    Dim hPrinter As Long
    Dim strDeviceName As String
    Dim strDevicePort As String
    Dim strBinNamesList As String
    Dim strBinName As String
    Dim intLength As Integer
    Dim strMsg As String
    Dim aintNumBin() As Integer

    On Error GoTo GetBinList_Err

    ' Get name and port of the default printer.
    strDeviceName = Application.Printers(Printer).DeviceName
    strDevicePort = Application.Printers(Printer).Port

    ' Get count of paper bin names supported by the printer.
    lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
        lpPort:=strDevicePort, _
        iIndex:=DC_BINNAMES, _
        lpOutput:=ByVal vbNullString, _
        lpDevMode:=DEFAULT_VALUES)

    ' Re-dimension the array to count of paper bins.
    ReDim aintNumBin(1 To lngBinCount)

    ' Pad variable to accept 24 bytes for each bin name.
    strBinNamesList = String(Number:=24 * lngBinCount, Character:=0)

    ' Get string buffer of paper bin names supported by the printer.
    lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
        lpPort:=strDevicePort, _
        iIndex:=DC_BINNAMES, _
        lpOutput:=ByVal strBinNamesList, _
        lpDevMode:=DEFAULT_VALUES)

    ' Get array of paper bin numbers supported by the printer.
    lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
        lpPort:=strDevicePort, _
        iIndex:=DC_BINS, _
        lpOutput:=aintNumBin(1), _
        lpDevMode:=0)

    ' List available paper bin names.
    strMsg = "Paper bins available for " & strDeviceName & vbCrLf
    For lngCounter = 1 To lngBinCount

        ' Parse a paper bin name from string buffer.
        strBinName = Mid(String:=strBinNamesList, _
            Start:=24 * (lngCounter - 1) + 1, _
            Length:=24)
        intLength = VBA.InStr(Start:=1, _
            String1:=strBinName, String2:=Chr(0)) - 1
        strBinName = Left(String:=strBinName, _
                Length:=intLength)

        If InStr(1, strBinName, BIN) > 0 Then
        ' Select the bin number corresponding to the bin name.
            BinSelection = aintNumBin(lngCounter)
        End If
     Next lngCounter


GetBinList_End:
    Exit Function
GetBinList_Err:
    MsgBox Prompt:=err.Description, Buttons:=vbCritical & vbOKOnly, _
        Title:="Error Number " & err.Number & " Occurred"
    Resume GetBinList_End
End Function

回答by SeanC

looks like you need to look out for .DefaultSize- if it's true, then your ItemSizeHeight& ItemSizeWidthsettings are ignored

看起来你需要注意.DefaultSize- 如果它是真的,那么你的ItemSizeHeight&ItemSizeWidth设置将被忽略

There's lots more information on MSDNand some examples

MSDN上有更多信息和一些示例