2012-07-04 40 views
7

Tôi đang xây dựng ứng dụng MS Access (2010) và tôi sẽ in nhãn mã vạch từ ứng dụng này. Có nhiều báo cáo và biểu mẫu khác cũng sẽ được in, sử dụng cài đặt máy in chuẩn, tuy nhiên với mã vạch tôi cần để in cho một máy in cụ thể và phải đặt để sử dụng kích thước trang đặc biệt.MS Access: khổ giấy tùy chỉnh

Trong tìm kiếm của mình, tôi có found the printer object's property "Papersize" có các tùy chọn mặc định "chuẩn" bao gồm acPRPSLetter cho Thư Hoa Kỳ chuẩn 8.5 "x 11" và acPRPSA4 cho khổ giấy A4. Không có kích thước cài sẵn nào sẽ hoạt động cho mục đích sử dụng của tôi. Có một giá trị đặt trước đại diện cho kích thước tùy chỉnh của người dùng, acPRPSUser, nhưng tôi không tìm thấy bất kỳ cách nào để lập trình đặt kích thước tùy chỉnh.

Tôi đã đọc về các thuộc tính '.height' và '.width' của máy in, nhưng dường như chúng không tồn tại trong VB được sử dụng cho Access 2010 (tôi tin nó dựa trên VB6) .

Có ai có thể giúp tôi đặt khổ giấy tùy chỉnh bằng mã VB trong Access 2010 không?

+0

Làm thế nào về http://msdn.microsoft.com/en-us/library/ff836635.aspx? – Fionnuala

+0

Tôi đã thấy điều đó và có nghĩa là đưa nó vào bài đăng của tôi. Cảm ơn bạn đã tìm thấy nó. Điều đó cho thấy thuộc tính '.papersize' đang được sử dụng, trong ví dụ nó đang sử dụng acPRPSLetter như đã đề cập ở trên. Đó là những gì tôi đang gọi, vì thiếu một thuật ngữ tốt hơn, một giá trị đặt trước. Tôi cần sử dụng kích thước tùy chỉnh. – CertifiedKennedy

+0

Cuối cùng của danh sách là acPRPSUser (http://msdn.microsoft.com/en-us/library/ff845524.aspx) – Fionnuala

Trả lời

2

Không cần VBA. Bạn có thể đặt tất cả cài đặt trang cho lề, hướng, giấy, máy in và cột bằng cách sử dụng lệnh cài đặt trang từ menu: Công cụ cho thiết kế báo cáo> Cài đặt trang> Cài đặt trang> Trang> Giấy> Kích cỡ hoặc > Trang> Máy in cho ReportName> Sử dụng máy in cụ thể> Máy in> Thuộc tính. Các cài đặt này được lưu cho mỗi báo cáo induvidual.

Screenshot

+3

Đó là gần hơn với những gì tôi đang tìm kiếm, nhưng không hoàn toàn. Tệp này sẽ được sử dụng trên các máy tính khác nhau, vì vậy cài đặt khổ giấy cần phải được cấu hình bằng tệp Access vì nó không thực tế từ quan điểm hỗ trợ để định cấu hình máy in theo cách thủ công trên từng máy riêng biệt. Tôi rất thích thiết lập nó theo lập trình nếu có thể, theo cách đó mỗi máy tính chạy tập tin này sẽ biết chính xác cách xử lý các nhãn. – CertifiedKennedy

0

vẻ như bạn cần phải tìm cho ra .DefaultSize - nếu đó là sự thật, sau đó ItemSizeHeight & ItemSizeWidth thiết lập của bạn sẽ được bỏ qua

Có rất nhiều thêm thông tin về MSDN và một số ví dụ

1

tôi đã vấn đề giống nhau. Tôi đã giải quyết vấn đề này bằng cách sử dụng How to: Programmatically Retrieve Printer Capabilities

Tôi đã tạo mô-đun với Quy trình in thủ tục. Với chức năng Printerselection, tôi có thể gọi một máy in sử dụng một phần cụ thể của printername. Chức năng PaperSelection được sử dụng để chỉ định giấy sử dụng một phần cụ thể của tên giấy.

Trước tiên tôi đã phải sử dụng tờ khai cho DeviceCapabilities chức năng gọi 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