2012-07-04 154 views
7

我正在構建MS Access(2010)應用程序,我將從此應用程序打印條形碼標籤。還有其他各種報告和表格,這些報告和表格也將使用標準打印機設置進行打印,但是我需要將其打印到特定打印機的條形碼,並且必須將其設置爲使用特殊的頁面大小。MS Access:自定義紙張尺寸

在我的搜索中,我有found the printer object's property「紙張大小」,它本身有許多「標準」默認選項,包括8.5英寸x 11英寸的標準美國信紙的acPRPSLetter和A4紙張大小的acPRPSA4。預設尺寸都不適用於我的使用。有一個代表用戶自定義大小的預設acPRPSUser,但我還沒有找到任何編程方式設置的自定義大小。

我的確瞭解了打印機的'.height'和'.width'屬性,但它並不出現在用於Access 2010的VB中(我相信它基於VB6) 。

任何人都可以幫助我在Access 2010中使用VB代碼設置自定義紙張大小?

+0

如何http://msdn.microsoft.com/en-us/library/ff836635.aspx? – Fionnuala

+0

我確實看到了這一點,並且打算將它包含在我的文章中。謝謝你找到它。這表明正在使用'.papersize'屬性,在上例中它使用acPRPSLetter。這就是我所說的,因爲缺少一個更好的術語,一個預設。我需要使用自定義大小。 – CertifiedKennedy

+0

最後一個列表是acPRPSUser(http://msdn.microsoft.com/en-us/library/ff845524.aspx) – Fionnuala

回答

2

不需要VBA。您可以使用菜單中的頁面設置命令設置邊距,方向,紙張,打印機和列的所有頁面設置:報表設計工具>頁面設置>頁面設置>頁面>紙張>尺寸> Page> ReportName的打印機>使用特定打印機>打印機>屬性。這些設置將保存在每個個人報告中。

Screenshot

+3

這與我所尋找的更接近,但並不完全。該文件將在不同的計算機上使用,因此紙張大小設置需要使用Access文件進行配置,因爲從支持的角度來看,在每臺計算機上分別手動配置打印機是不切實際的。我希望儘可能以編程方式進行設置,這樣每個運行此文件的計算機都將知道如何處理標籤。 – CertifiedKennedy

0

看起來像你需要尋找出.DefaultSize - 如果這是真的,那麼你的ItemSizeHeight & ItemSizeWidth設置被忽略

有很多關於MSDN和更多信息的一些例子

1

我一樣的問題。我通過使用How to: Programmatically Retrieve Printer Capabilities

解決了問題我用一個過程打印輸出了一個模塊。使用功能Printerselection,我可以使用printername的特定部分調用打印機。 PaperSelection功能用於使用紙張名稱的特定部分指定紙張。

首先,我不得不使用聲明的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