CommonDialog.ShowPrinter的设置结果并不和printer对象关联,由于CommonDialog.ShowPrinter并不输出其纸张类型,只能通过变通了(如果不使用对话框就很容易解决了),在窗体里添加一个CommonDialog1控件和一个command1控件,输入如下代码:
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Sub Command1_Click()
Dim h As Long, w As Long, h1 As Long, w1 As Long, i As Integer, j As Integer
On Error Resume Next
CommonDialog1.CancelError = True
CommonDialog1.Flags = cdlPDPrintSetup + cdlPDReturnDC
CommonDialog1.ShowPrinter
If Err.Number = 32755 Then Exit Sub
'选择了取消
w = GetDeviceCaps(CommonDialog1.hDC, 110)
'获取CommonDialog1打印纸宽
h = GetDeviceCaps(CommonDialog1.hDC, 111)
'获取CommonDialog1打印纸高
Printer.ScaleMode = 3: j = -1
For i = 0 To 255
Err.Clear
Printer.PaperSize = i
If Err.Number = 0 Then
w1 = GetDeviceCaps(Printer.hDC, 110)
'获取Printer打印纸宽
h1 = GetDeviceCaps(Printer.hDC, 111)
'获取Printer打印纸高
If w1 = w And h1 = h Then
j = i
Exit For
End If
End If
Next
If j > 0 Then
MsgBox "打印纸尺寸类型为" & j & ",代码为: Printer.PaperSize=" & j
Else
MsgBox "出错"
End If
'Me.PrintForm
'打印窗体
'Printer.EndDoc
'结束打印
End Sub