问:
因為我的專案須要很多種報表格式 如 A4,LETTER SIZE,我使用 VB的 PRINTER.PAPERSIZE 去更改設定但是沒有作用  !!!!想請教各位先進,有那一個 C++ OR API OR VB 指令可以動態設定印表機的狀態。

答:
你不需要 C++ or API ,你只須要在 VB 裏做出正確的設定,一定可以用 Printer.PaperSize 去更改紙張設定,而所謂正確設定,應該包含下列幾件事:
Set Default Printer 的動作要正確,如 VB 說明:
如果用Printers 集合物件來確定某一特定印表機,如 Printers(3),則只能存取唯讀屬性。如果想存取個別印表機的可讀寫屬性,首先要使那個印表機成為應用程式的預設印表機。
你可以參考下面範例 SetDefaultPrinter 的作法。
VB Printer 物件共支援 41 項紙張設定,連 UserType 共 42 項,但並不是每個印表機驅動程式都支援全部的紙張格式,所以在設定紙張格式前,你必須先測試目前 DefaultPrinter 能使用的紙張格式,請參考下面範例的 WhatPaperCanUse 副程式。
許多 VB 物件與電腦週邊設備的溝通,如果使用錯誤處理程序,也就是不管週邊支不支援,先加以驅動,再依傳回訊息處理,就可以不需使用 API 或其他語言的物件,在VB 內把問題解決掉。
此外比必須注意,當你改變了印表機 Height 和 Width 屬性設定時會自動將 PaperSize 設定為 vbPRPSUser(使用者自訂)。
'--------------------  印表機設定範例開始  --------------------------
VERSION 5.00
Begin VB.Form frmPrinterSet
   Caption         =   "印表機設定表單"
   ClientHeight    =   4245
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6210
   LinkTopic       =   "Form1"
   ScaleHeight     =   4245
   ScaleWidth      =   6210
   StartUpPosition =   3  '系統預設值
   Begin VB.ListBox List2
      Height          =   1860
      Left            =   3000
      TabIndex        =   3
      Top             =   840
      Width           =   2895
   End
   Begin VB.ListBox List1
      Height          =   1860
      Left            =   240
      TabIndex        =   0
      Top             =   840
      Width           =   2535
   End
   Begin VB.Label Label6
      BackColor       =   &H80000009&
      BorderStyle     =   1  '單線固定
      Height          =   375
      Left            =   4200
      TabIndex        =   7
      Top             =   3600
      Width           =   1695
   End
   Begin VB.Label Label5
      Caption         =   "紙張高度(單位 釐米)"
      Height          =   375
      Left            =   4200
      TabIndex        =   6
      Top             =   3000
      Width           =   1695
   End
   Begin VB.Label Label4
      BackColor       =   &H80000009&
      BorderStyle     =   1  '單線固定
      Height          =   375
      Left            =   2160
      TabIndex        =   5
      Top             =   3600
      Width           =   1695
   End
   Begin VB.Label Label3
      Caption         =   "紙張寬度(單位 釐米)"
      Height          =   375
      Left            =   2160
      TabIndex        =   4
      Top             =   3000
      Width           =   1695
   End
   Begin VB.Label Label2
      Alignment       =   2  '靠中對齊
      Caption         =   "可使用紙張格式"
      Height          =   375
      Left            =   3360
      TabIndex        =   2
      Top             =   240
      Width           =   2175
   End
   Begin VB.Label Label1
      Alignment       =   2  '靠中對齊
      Caption         =   "印表機型號"
      Height          =   375
      Left            =   480
      TabIndex        =   1
      Top             =   240
      Width           =   2055
   End
End
Attribute VB_Name = "frmPrinterSet"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'所有紙張型號的儲存陣列
Dim PaperStyle(1 To 41) As String
'印表機參照變數
Dim XPrint As Printer
'把所有 Printer 物件能支援的紙張格式說明放入陣列中
Private Sub GotPaperStyle()
    PaperStyle(vbPRPSLetter) = "信箋, 8 1/2 x 11 英吋。"
    PaperStyle(vbPRPSLetterSmall) = "小型信箋, 8 1/2 x 11 英吋。"
    PaperStyle(vbPRPSTabloid) = "小型報, 11 x 17 英吋。"
    PaperStyle(vbPRPSLedger) = "分類帳, 17 x 11 英吋。"
    PaperStyle(vbPRPSLegal) = "法律檔案, 8 1/2 x 14 英吋。"
    PaperStyle(vbPRPSStatement) = "宣告書,5 1/2 x 8 1/2 英吋。"
    PaperStyle(vbPRPSExecutive) = "行政檔案,7 1/2 x 10 1/2 英吋。"
    PaperStyle(vbPRPSA3) = "A3, 297 x 420 公厘"
    PaperStyle(vbPRPSA4) = "A4, 210 x 297 公厘"
    PaperStyle(vbPRPSA4Small) = "A4小號, 210 x 297 公厘"
    PaperStyle(vbPRPSA5) = "A5, 148 x 210 公厘"
    PaperStyle(vbPRPSB4) = "B4, 250 x 354 公厘"
    PaperStyle(vbPRPSB5) = "B5, 182 x 257 公厘"
    PaperStyle(vbPRPSFolio) = "對開本, 8 1/2 x 13 英吋。"
    PaperStyle(vbPRPSQuarto) = "四開本, 215 x 275 公厘。"
    PaperStyle(vbPRPS10x14) = "10 x 14 英吋。"
    PaperStyle(vbPRPS11x17) = "11 x 17 英吋。"
    PaperStyle(vbPRPSNote) = "便條,8 1/2 x 11 英吋。"
    PaperStyle(vbPRPSEnv9) = "#9 信封, 3 7/8 x 8 7/8 英吋。"
    PaperStyle(vbPRPSEnv10) = "#10 信封, 4 1/8 x 9 1/2 英吋。"
    PaperStyle(vbPRPSEnv11) = "#11 信封, 4 1/2 x 10 3/8 英吋。"
    PaperStyle(vbPRPSEnv12) = "#12 信封, 4 1/2 x 11 英吋。"
    PaperStyle(vbPRPSEnv14) = "#14 信封, 5 x 11 1/2 英吋。"
    PaperStyle(vbPRPSCSheet) = "C 尺寸工作單"
    PaperStyle(vbPRPSDSheet) = "D 尺寸工作單"
    PaperStyle(vbPRPSESheet) = "E 尺寸工作單"
    PaperStyle(vbPRPSEnvDL) = "DL 型信封, 110 x 220 公厘"
    PaperStyle(vbPRPSEnvC3) = "C3 型信封, 324 x 458 公厘"
    PaperStyle(vbPRPSEnvC4) = "C4 型信封, 229 x 324 公厘"
    PaperStyle(vbPRPSEnvC5) = "C5 型信封, 162 x 229 公厘"
    PaperStyle(vbPRPSEnvC6) = "C6 型信封, 114 x 162 公厘"
    PaperStyle(vbPRPSEnvC65) = "C65 型信封,114 x 229 公厘"
    PaperStyle(vbPRPSEnvB4) = "B4 型信封, 250 x 353 公厘"
    PaperStyle(vbPRPSEnvB5) = "B5 型信封,176 x 250 公厘"
    PaperStyle(vbPRPSEnvB6) = "B6 型信封, 176 x 125 公厘"
    PaperStyle(vbPRPSEnvItaly) = "信封, 110 x 230 公厘"
    PaperStyle(vbPRPSEnvMonarch) = "信封大王, 3 7/8 x 7 1/2 英吋。"
    PaperStyle(vbPRPSEnvPersonal) = "信封, 3 5/8 x 6 1/2 英吋。"
    PaperStyle(vbPRPSFanfoldUS) = "U.S. 標準複寫簿, 14 7/8 x 11 英吋。"
    PaperStyle(vbPRPSFanfoldStdGerman) = "德國標準複寫簿, 8 1/2 x 12 英吋。"
    PaperStyle(vbPRPSFanfoldLglGerman) = "德國法律複寫簿, 8 1/2 x 13 英吋。"
End Sub
Private Sub Form_Load()
Dim TempPrint As Printer
    '列出本機可使用的所有印表機
    For Each TempPrint In Printers
        List1.AddItem TempPrint.DeviceName
    Next

    '執行把紙張格式說明放入陣列中的自訂副程式
    GotPaperStyle
End Sub
Private Sub List1_Click()
    '依 List1 Item 設定 DefaultPrinter,並依  DefaultPrinter 找出可支援的紙
張格式。
    If List1.ListIndex >= 0 Then
        '設定 DefaultPrinter副程式
        SetDefaultPrinter List1.List(List1.ListIndex)
        '找出支援紙張格式副程式
        WhatPaperCanUse
    End If
End Sub
'設定 DefaultPrinter副程式
Private Sub SetDefaultPrinter(ByVal DName As String)
    For Each XPrint In Printers
        If XPrint.DeviceName = DName Then
            Set Printer = XPrint
            Exit For
        End If
    Next
End Sub
'找出支援紙張格式副程式
Private Sub WhatPaperCanUse()
Dim I As Long

    On Error Resume Next
    List2.Clear

    For I = LBound(PaperStyle) To UBound(PaperStyle)
        '清除錯誤碼
        Err.Number = 0
        '本程式是先設定紙張格式,再觀察是否有誤,只有無錯誤者才加入 List2 中
        Printer.PaperSize = I
        Select Case Err.Number
            Case 0
                List2.AddItem PaperStyle(I)
                List2.ItemData(List2.NewIndex) = I
            Case 380
            Case Else
                MsgBox Err.Description, vbOKOnly, "錯誤代號:" & Err.Number
                Exit For
        End Select
    Next
End Sub
'依紙張格式顯示紙張長寬
Private Sub List2_Click()
    If List2.ListIndex >= 0 Then
        Printer.PaperSize = List2.ItemData(List2.ListIndex)
        Label4.Caption = Printer.ScaleX(Printer.Width, Printer.ScaleMode,vbMillimeters)
        Label6.Caption = Printer.ScaleY(Printer.Height, Printer.ScaleMode,vbMillimeters)
        '因為不要印出,所以把列印序列殺掉。
        Printer.KillDoc
    End If
End Sub
'--------------------  印表機設定範例結束  --------------------------
posted on 2005-01-19 09:36  James Wong   阅读(2450)  评论(0编辑  收藏  举报