2011-04-02 74 views
1

請在選擇所有打印機紙張尺寸時使用其名稱包含在vb6中。我已經可以選擇使用此代碼的所有打印機,並將其放入一個列表框中。WMI查詢選擇所有打印機紙張尺寸vb6

Set WMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2") 
Set Items = WMIService.ExecQuery("Select * from Win32_Printer", , 48) 

我需要的是選擇打印機我選擇的所有紙張尺寸/名字,並把它放在另一個列表框

回答

1

WMI代碼是一個管理腳本的服務,應用程序不應該依賴存在並跑步。儘管有直接獲取信息的完美方法。

這是兩個ListBox一個樣表:

Option Explicit 

Private Const DC_PAPERNAMES = 16 

Private Declare Function DeviceCapabilities Lib "winspool.drv" _ 
    Alias "DeviceCapabilitiesW" (_ 
    ByVal lpDeviceName As Long, _ 
    ByVal lpPort As Long, _ 
    ByVal iIndex As Long, _ 
    ByVal lpOutput As Long, _ 
    ByVal lpDevMode As Long) As Long 

Private Sub Form_Load() 
    Dim P As Printer 

    For Each P In Printers 
     lstPrinters.AddItem P.DeviceName 
    Next 
End Sub 

Private Sub lstPrinters_Click() 
    Dim P As Printer 
    Dim lngPapers As Long 
    Dim strPaperNames As String 
    Dim lngPaper As Long 
    Dim strPaperName As String 
    Dim lngActualLength As Long 

    Set P = Printers(lstPrinters.ListIndex) 
    lngPapers = DeviceCapabilities(StrPtr(P.DeviceName), _ 
            StrPtr(P.Port), _ 
            DC_PAPERNAMES, _ 
            0, _ 
            0) 
    strPaperNames = String$(lngPapers * 64, 0) 
    lngPapers = DeviceCapabilities(StrPtr(P.DeviceName), _ 
            StrPtr(P.Port), _ 
            DC_PAPERNAMES, _ 
            StrPtr(strPaperNames), _ 
            0) 
    lstPapers.Clear 
    For lngPaper = 0 To lngPapers - 1 
     strPaperName = Mid$(strPaperNames, 64 * lngPaper + 1, 64) 
     lngActualLength = InStr(strPaperName, vbNullChar) - 1 
     If lngActualLength > 1 Then strPaperName = Left$(strPaperName, lngActualLength) 
     lstPapers.AddItem strPaperName 
    Next 
End Sub 

你也可以使用類似呼叫檢索「紙張大小碼」或尺寸毫米。見DeviceCapabilities Function

+0

當然,如果你想*從管理腳本中做這樣的事情,WMI將是最好的選擇。 – Bob77 2011-04-02 19:46:05

+0

你的代碼輸出紙張大小爲1的系列,你能幫我看看實際的尺寸,例如紙張尺寸1是用於'8-1/2 x 11',我需要得到這個代替我已經檢查過的1s系列 – Smith 2011-04-03 17:57:40

1

Smith,您只需要訪問Win32_Printer wmi類的PaperSizesSupported和/或PaperTypesAvailable屬性,這兩個屬性都是數組。

+0

,而且似乎無法找出辦法。如果你能幫助我,那我會很感激 – Smith 2011-04-02 15:36:39

相關問題