Khi chay thử em thấy vẫn còn thiếu báo lỗi tí : Em cố tình tắt máy in đi, khi bấm print thì nó vẫn chạy bình thường.
Ý em là làm sao khi bấm Print, nếu không thể in được bằng máy in ta vừa chọn thì ra thông báo yêu cầu chọn máy in khác.
Mình đi theo hướng khác được không? Nghĩa là như thế này:
Search tìm những máy in nào có kết nối với máy tính của mình trong danh sách máy in của windows,
sau đó mới đưa vào combobox. Như vậy
Combobox sẽ chỉ chứa danh sách các máy in đã kết nối với máy tính.
Hoàng Danh tham khảo code sau (test lại vì ca_dafi không có máy in bên cạnh nhé)
[highlight=vb]
Option Explicit
Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2
Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" _
(ByVal flags As Long, ByVal name As String, ByVal Level As Long, _
pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, _
pcReturned As Long) As Long
Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
(ByVal RetVal As String, ByVal Ptr As Long) As Long
Private Declare Function StrLen Lib "kernel32" Alias "lstrlenA" _
(ByVal Ptr As Long) As Long
'\\------------------------------------------------------------------------------------
Public Function ListPrinters() As Variant
Dim bSuccess As Boolean
Dim iBufferRequired As Long
Dim iBufferSize As Long
Dim iBuffer() As Long
Dim iEntries As Long
Dim iIndex As Long
Dim strPrinterName As String
Dim iDummy As Long
Dim iDriverBuffer() As Long
Dim StrPrinters() As String
iBufferSize = 3072
ReDim iBuffer((iBufferSize \ 4) - 1) As Long
'EnumPrinters will return a value False if the buffer is not big enough
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, vbNullString, _
1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
If Not bSuccess Then
If iBufferRequired > iBufferSize Then
iBufferSize = iBufferRequired
Debug.Print "iBuffer too small. Trying again with "; _
iBufferSize & " bytes."
ReDim iBuffer(iBufferSize \ 4) As Long
End If
'Try again with new buffer
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, vbNullString, _
1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
End If
If Not bSuccess Then
'Enumprinters returned False
MsgBox "Error enumerating printers."
Exit Function
Else
'Enumprinters returned True, use found printers to fill the array
ReDim StrPrinters(iEntries - 1)
For iIndex = 0 To iEntries - 1
'Get the printername
strPrinterName = Space$(StrLen(iBuffer(iIndex * 4 + 2)))
iDummy = PtrToStr(strPrinterName, iBuffer(iIndex * 4 + 2))
StrPrinters(iIndex) = strPrinterName
Next iIndex
End If
ListPrinters = StrPrinters
End Function
'\\------------------------------------------------------------------------------------
''You could call the function as follows:
Public Function IsBounded(vArray As Variant) As Boolean
''If the variant passed to this function is an array, the function will return True;
''otherwise it will return False
On Error Resume Next
IsBounded = IsNumeric(UBound(vArray))
End Function[/highlight]
Và Code cho Form:
[highlight=vb]
Private Sub cmdPrint_Click()
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:=Me.cboPrintList.Value, Collate:=True
End Sub
Private Sub UserForm_Activate()
cboPrintList.ColumnCount = 1
cboPrintList.ColumnHeads = False
Dim StrPrinters As Variant, iRow As Long
StrPrinters = ListPrinters
If IsBounded(StrPrinters) Then
For iRow = LBound(StrPrinters) To UBound(StrPrinters)
cboPrintList.AddItem StrPrinters(iRow)
Next iRow
End If
End Sub
[/highlight]Tham khảo thêm tại đây:
http://word.mvps.org/FAQs/MacrosVBA/AvailablePrinters.htm
Và tại đây:
http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=183