Bạn thử:Các bác cho em hỏi tí.
Em muốn lấy danh sách máy in với tên đầy đủ ví dụ như:
- RICOH Aficio MP 6001 PCL 6 on Ne00:
- pdfFactory Pro on FPP5:
- Canon LBP2900 on Ne03:
thì phải viết như thế nào ạ?
Sau một thời gian Copy Copy ... Paste Paste thì cũng được cái đoạn loàng ngoàng nàyCác bác cho em hỏi tí.
Em muốn lấy danh sách máy in với tên đầy đủ ví dụ như:
- RICOH Aficio MP 6001 PCL 6 on Ne00:
- pdfFactory Pro on FPP5:
- Canon LBP2900 on Ne03:
thì phải viết như thế nào ạ?
Sub List_Printer()
Dim aPrinters As Object
Dim I As Long, Arr(), n As Long, Str As String
With CreateObject("WScript.Network")
Set aPrinters = .EnumPrinterConnections
For I = 1 To aPrinters.Count Step 2
n = n + 1
ReDim Preserve Arr(1 To n)
Arr(n) = aPrinters.Item(I)
Next
End With
If n Then
Str = Join(Arr, Chr(10))
MsgBox Str
Else
MsgBox "Nothing"
End If
End Sub
Cảm ơn bạn. Nhưng code này chỉ cho tên máy in chứ không có cổng máy in.Sau một thời gian Copy Copy ... Paste Paste thì cũng được cái đoạn loàng ngoàng này
Bạn tham khảo thửMã:Sub List_Printer() Dim aPrinters As Object Dim I As Long, Arr(), n As Long, Str As String With CreateObject("WScript.Network") Set aPrinters = .EnumPrinterConnections For I = 1 To aPrinters.Count Step 2 n = n + 1 ReDim Preserve Arr(1 To n) Arr(n) = aPrinters.Item(I) Next End With If n Then Str = Join(Arr, Chr(10)) MsgBox Str Else MsgBox "Nothing" End If End Sub
Thử code này xem:Cảm ơn bạn. Nhưng code này chỉ cho tên máy in chứ không có cổng máy in.
Nếu chạy code này chỉ có:
RICOH Aficio MP 6001 PCL 6
pdfFactory Pro
Canon LBP2900
ý mình là muốn danh sách máy in gồm cả cổng on nữa. ví dụ như:
- RICOH Aficio MP 6001 PCL 6 on Ne00:
- pdfFactory Pro on FPP5:
- Canon LBP2900 on Ne03:
Mong bạn giúp với
Public Const HKEY_CLASSES_ROOT As Long = &H80000000
Public Const HKEY_CURRENT_USER As Long = &H80000001
'Public Const HKEY_LOCAL_MACHINE As Long = &H80000002
'Public Const HKEY_USERS As Long = &H80000003
'Public Const HKEY_PERFORMANCE_DATA As Long = &H80000004
'Public Const HKEY_CURRENT_CONFIG As Long = &H80000005
'Public Const HKEY_DYN_DATA As Long = &H80000006
Public Const WMI_CLASS As String = "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv"
Public Const DEVICES As String = "Software\Microsoft\Windows NT\CurrentVersion\Devices"
Function GetAllPrinters()
Dim arr()
Dim prn
Dim aPrinters
Dim prnName As String
Dim regValue As String
Dim n As Long
With GetObject(WMI_CLASS)
.enumvalues HKEY_CURRENT_USER, DEVICES, aPrinters
For Each prn In aPrinters
.getstringvalue HKEY_CURRENT_USER, DEVICES, prn, regValue
prnName = prn & " on " & Split(regValue, ",")(1)
n = n + 1
ReDim Preserve arr(1 To n)
arr(n) = prnName
'Debug.Print prnName
Next
End With
If n Then
GetAllPrinters = arr
End If
End Function
Code đó không đúng yêu cầu của người ta đâu bạn à, vì kết quả thiếu phần đươi On Ne00:, On Ne01:.... vân vân...
Code chạy lên thì thấy như hình. Có đầy đủ On Ne00: .... luôn màCode đó không đúng yêu cầu của người ta đâu bạn à, vì kết quả thiếu phần đươi On Ne00:, On Ne01:.... vân vân...
Cảm ơn bác nhiều ạThử code này xem:
Gõ vào cell nào đó trên bảng tính công thức: =GetAllPrinters() rồi Ctrl + Shift + Enter. Bấm F2 rồi F9 sẽ thấy các phần tử trong mảngMã:Public Const HKEY_CLASSES_ROOT As Long = &H80000000 Public Const HKEY_CURRENT_USER As Long = &H80000001 'Public Const HKEY_LOCAL_MACHINE As Long = &H80000002 'Public Const HKEY_USERS As Long = &H80000003 'Public Const HKEY_PERFORMANCE_DATA As Long = &H80000004 'Public Const HKEY_CURRENT_CONFIG As Long = &H80000005 'Public Const HKEY_DYN_DATA As Long = &H80000006 Public Const WMI_CLASS As String = "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv" Public Const DEVICES As String = "Software\Microsoft\Windows NT\CurrentVersion\Devices" Function GetAllPrinters() Dim arr() Dim prn Dim aPrinters Dim prnName As String Dim regValue As String Dim n As Long With GetObject(WMI_CLASS) .enumvalues HKEY_CURRENT_USER, DEVICES, aPrinters For Each prn In aPrinters .getstringvalue HKEY_CURRENT_USER, DEVICES, prn, regValue prnName = prn & " on " & Split(regValue, ",")(1) n = n + 1 ReDim Preserve arr(1 To n) arr(n) = prnName 'Debug.Print prnName Next End With If n Then GetAllPrinters = arr End If End Function
-------------
Tặng luôn file có form làm sẵn (bằng Dialog sheet)
Sao máy em nó không ra On Ne0... nhỉ? Bác cho em xin code bác chạy với. Em muốn thử nhiều phương án màCode chạy lên thì thấy như hình. Có đầy đủ On Ne00: .... luôn mà
Sao máy em nó không ra On Ne0... nhỉ? Bác cho em xin code bác chạy với. Em muốn thử nhiều phương án mà
Option Explicit
Private Declare Function GetProfileString Lib "kernel32" _
Alias "GetProfileStringA" (ByVal lpAppName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long
Sub Test()
Dim vaList
'Get all printers
vaList = PrinterFind
'Show m
MsgBox Join(vaList, vbLf), , "List of printers"
'Get all laserjets
vaList = PrinterFind(Match:="Laserjet")
'Switch to the first laserjet found
If UBound(vaList) = -1 Then
MsgBox "Printer not found"
ElseIf MsgBox("from " & vbTab & ": " & ActivePrinter & vbLf & _
"to " & vbTab & ": " & vaList(0), _
vbOKCancel, "Switch Printers") = vbOK Then
Application.ActivePrinter = vaList(0)
End If
End Sub
Public Function PrinterFind(Optional Match As String) As String()
Dim n%, lRet&, sBuf$, sCon$, aPrn$()
Const lLen& = 1024, sKey$ = "devices"
'------------------------------------------------------------------
'written by keepITcool
'requires xl2000 or newer.
'returns a zerobased array of complete localized printer strings
'results are filtered on Match string, if no result the ubound = -1
'------------------------------------------------------------------
'Split ActivePrinter string to get localized word for "on"
aPrn = Split(Excel.ActivePrinter)
sCon = " " & aPrn(UBound(aPrn) - 1) & " "
'Read all installed printers (1k bytes s/b enough)
sBuf = Space(lLen)
lRet = GetProfileString(sKey, vbNullString, vbNullString, sBuf, lLen)
If lRet = 0 Then
Err.Raise vbObjectError + 513, , "Can't read Profile"
Exit Function
End If
'Split buffer string
aPrn = Split(Left(sBuf, lRet - 1), vbNullChar)
'Filter array on Match
If Match <> vbNullString Then aPrn = Filter(aPrn, Match, -1, 1)
For n = LBound(aPrn) To UBound(aPrn)
'Add 16bit portname for each Printer
sBuf = Space(lLen)
lRet = GetProfileString(sKey, aPrn(n), vbNullString, sBuf, lLen)
aPrn(n) = aPrn(n) & sCon & _
Mid(sBuf, InStr(sBuf, ",") + 1, lRet - InStr(sBuf, ","))
Next
'Return the result
PrinterFind = aPrn
End Function
Cảm ơn bác nhéMã:Option Explicit Private Declare Function GetProfileString Lib "kernel32" _ Alias "GetProfileStringA" (ByVal lpAppName As String, _ ByVal lpKeyName As String, ByVal lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Long) As Long Sub Test() Dim vaList 'Get all printers vaList = PrinterFind 'Show m MsgBox Join(vaList, vbLf), , "List of printers" 'Get all laserjets vaList = PrinterFind(Match:="Laserjet") 'Switch to the first laserjet found If UBound(vaList) = -1 Then MsgBox "Printer not found" ElseIf MsgBox("from " & vbTab & ": " & ActivePrinter & vbLf & _ "to " & vbTab & ": " & vaList(0), _ vbOKCancel, "Switch Printers") = vbOK Then Application.ActivePrinter = vaList(0) End If End Sub Public Function PrinterFind(Optional Match As String) As String() Dim n%, lRet&, sBuf$, sCon$, aPrn$() Const lLen& = 1024, sKey$ = "devices" '------------------------------------------------------------------ 'written by keepITcool 'requires xl2000 or newer. 'returns a zerobased array of complete localized printer strings 'results are filtered on Match string, if no result the ubound = -1 '------------------------------------------------------------------ 'Split ActivePrinter string to get localized word for "on" aPrn = Split(Excel.ActivePrinter) sCon = " " & aPrn(UBound(aPrn) - 1) & " " 'Read all installed printers (1k bytes s/b enough) sBuf = Space(lLen) lRet = GetProfileString(sKey, vbNullString, vbNullString, sBuf, lLen) If lRet = 0 Then Err.Raise vbObjectError + 513, , "Can't read Profile" Exit Function End If 'Split buffer string aPrn = Split(Left(sBuf, lRet - 1), vbNullChar) 'Filter array on Match If Match <> vbNullString Then aPrn = Filter(aPrn, Match, -1, 1) For n = LBound(aPrn) To UBound(aPrn) 'Add 16bit portname for each Printer sBuf = Space(lLen) lRet = GetProfileString(sKey, aPrn(n), vbNullString, sBuf, lLen) aPrn(n) = aPrn(n) & sCon & _ Mid(sBuf, InStr(sBuf, ",") + 1, lRet - InStr(sBuf, ",")) Next 'Return the result PrinterFind = aPrn End Function
Thầy cho em hỏi, có phải cái On Ne1, On Ne2, On Ne3 mỗi lần tắt máy tính mở lại có thể nó bị thay đổi ko ạ. Ví dụ như hôm qua em thấy nó hiện là Canon 2900 on Ne02, Hôm nay lại thấy là Canon 2900 On Ne03Thử code này xem:
Gõ vào cell nào đó trên bảng tính công thức: =GetAllPrinters() rồi Ctrl + Shift + Enter. Bấm F2 rồi F9 sẽ thấy các phần tử trong mảngMã:Public Const HKEY_CLASSES_ROOT As Long = &H80000000 Public Const HKEY_CURRENT_USER As Long = &H80000001 'Public Const HKEY_LOCAL_MACHINE As Long = &H80000002 'Public Const HKEY_USERS As Long = &H80000003 'Public Const HKEY_PERFORMANCE_DATA As Long = &H80000004 'Public Const HKEY_CURRENT_CONFIG As Long = &H80000005 'Public Const HKEY_DYN_DATA As Long = &H80000006 Public Const WMI_CLASS As String = "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv" Public Const DEVICES As String = "Software\Microsoft\Windows NT\CurrentVersion\Devices" Function GetAllPrinters() Dim arr() Dim prn Dim aPrinters Dim prnName As String Dim regValue As String Dim n As Long With GetObject(WMI_CLASS) .enumvalues HKEY_CURRENT_USER, DEVICES, aPrinters For Each prn In aPrinters .getstringvalue HKEY_CURRENT_USER, DEVICES, prn, regValue prnName = prn & " on " & Split(regValue, ",")(1) n = n + 1 ReDim Preserve arr(1 To n) arr(n) = prnName 'Debug.Print prnName Next End With If n Then GetAllPrinters = arr End If End Function
-------------
Tặng luôn file có form làm sẵn (bằng Dialog sheet)
Thay đổi thì kệ nó đi! Đằng nào thì mấy cái On Ne01, Ne02... gì gì đó cũng đâu phải do mình tự ghi mà lo. Cái chuỗi ấy ta lấy trong Registry màThầy cho em hỏi, có phải cái On Ne1, On Ne2, On Ne3 mỗi lần tắt máy tính mở lại có thể nó bị thay đổi ko ạ. Ví dụ như hôm qua em thấy nó hiện là Canon 2900 on Ne02, Hôm nay lại thấy là Canon 2900 On Ne03
Thay đổi thì kệ nó đi! Đằng nào thì mấy cái On Ne01, Ne02... gì gì đó cũng đâu phải do mình tự ghi mà lo. Cái chuỗi ấy ta lấy trong Registry mà