解决 莫名其妙的问题Printers.Count 2 ,改用API函数 获得默认打印机,枚举打印机等

Public Sub WinNTGetDefaultPrinter(PrinterName As String)
    Dim Buffer As String
    Dim DeviceName As String
    Dim DriverName As String
    Dim PrinterPort As Stringwww.yippeesoft.com
\’    Dim PrinterName As String
    Dim r As Long

    \’ Get the printer information for the currently selected
    \’ printer in the list. The information is taken from the
    \’ WIN.INI file.
    Buffer = Space(1024)

    r = GetProfileString("Windows", "device", "", _
        Buffer, Len(Buffer))www.yippeesoft.com

    \’ Parse the driver name anwww.yippeesoft.comd port name out of the buffer
    GetDriverAndPort Buffer, DriverName, PrinterPort

    PrinterName = DriverName
End Sub

 Public Sub EnumeratePrinters4(iprts As Integer, str() As String)
      Dim Success As Boolean, cbRequired As Long, cbBuffer As Long
      Dim Buffer() As Long, nEntries As Long
      Dim i As Long, PName As String, SName As String
      Dim Attrib As Long, Temp As Long
         cbBuffer = 3072
         ReDim Buffer((cbBuffer \\ 4) – 1) As Long
         Success = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
                               PRINTER_ENUM_LOCAL, _
                               vbNullString, _
                               4, _
                               Buffer(0), _
                               cbBuffer, _
                               cbRequired, _www.yippeesoft.com
                               nEntries)
         If Success Then
            If cbRequired > cbBuffer Then
               cbBuffer = cbRequired
               Debug.Print "Buffer too small.  Trying again with " & _
                        cbBuffer & " bytes."
               ReDim Buffer(cbBuffer \\ 4) As Long
               Success = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
                                   PRINTER_ENUM_LOCAL, _
                                   vbNullString, _
                                   4, _www.yippeesoft.com
                                   Buffer(0), _
                                   cbBuffer, _
                                   cbRequired, _
                                   nEntries)
               If Not Success Then
                  Debug.Print "Error enumerating printers."
                  Exit Sub
               End If
            End If
            Debug.Print "There are " & nEntries & _
                      " local and connected printers."
            iprts = nEntrieswww.yippeesoft.com
            ReDim str(nEntries)
            For i = 0 To nEntries – 1
                PName = Space$(StrLen(Buffer(i * 3)))
                Temp = PtrToStr(PName, Buffer(i * 3))
                SName = Space$(StrLen(Buffer(i * 3 + 1)))
                Temp = PtrToStr(SName, Buffer(i * 3 + 1))
                Attrib = Buffer(i * 3 + 2)
                str(i) = PName
            \’MsgBox "Printer: " & PName \’ + "Server: " & Str(SName) + "Attributes: " & Str(Hex$(Attrib))
            Next i
         Else
            iprts = 0
         End If
      End Sub

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
266767 (http://support.microsoft.com/kb/266767/EN-US/) HOWTO: Set Which Printer Is the System Default Printer
142388 (http://support.microsoft.com/kb/142388/EN-US/) HOWTO: Changing WIN.INI Printer Settings from VB using Windows API

For additional information on determining which 32-Bit for Windows NT 3.51, Windows NT 4.0 and Windows 95, click the article number below to view the article in the Microsoft Knowledge Base:

189249 (http://support.microsoft.com/kb/189249/EN-US/) HOWTO: Determine Which 32-Bit Windows Version Is Being Used
140560 (http://support.microsoft.com/kb/140560/EN-US/) HOWTO: Set the Default Printer Programmatically in Windows 95, Windows 98, or Windows Me
167735 (http://support.microsoft.com/kb/167735/EN-US/) FIX: Setting Printer to Item in the Printers Collection Fails
253612 (http://support.microsoft.com/kb/253612/EN-US/) FIX: Printers Collection May Not Contain All Printers in the Printers Folder
246772 (http://support.microsoft.com/kb/246772/EN-US/) How To Retrieve and Set the Default Printer in Windows

历史博文

标签:, , ,
七月 25, 2005 at 3:07 下午 by yippee 1,004 次
Category: Dev
Tags: , , ,