Thứ Sáu, 23 tháng 11, 2012

Dùng khung hiển thị danh sách (ListView) với VBA

Trong ví dụ dưới đây, ta sẽ tạo một ứng dụng để in các tập tin excel. Hộp thoại của ứng dụng gồm :
- Một hộp nhập liệu. Nút Browse cho phép chọn thư mục chứa các tập tin excel. Khi chọn xong, đường dẫn này hiển thị trong hộp nhập liệu, và các tập tin excel xuất hiện trong Khung hiển thị danh sách (ListView)
- Khung hiển thị danh sách với thuộc tính (property) Checkboxes có giá trị True cho phép hiển thị các hộp chọn trước mỗi tập tin. Và thuộc tính View có giá trị 2-lvwList để hiển thị nội dung dưới dạng danh sách.


Dưới đây là mã nguồn của hộp thoại trên :
Option Explicit

Private Sub chbSelectAll_Click()
    Dim item As Object
    If ListViewFiles.ListItems.Count > 0 Then
        If chbSelectAll.Value = True Then
            ' select all files
            For Each item In ListViewFiles.ListItems
                item.Checked = True
            Next
        ElseIf chbSelectAll.Value = False Then
            ' deselect all files
            For Each item In ListViewFiles.ListItems
                item.Checked = False
            Next
        End If
    End If
End Sub

Private Sub cmdBrowse_Click()
    Dim fd As FileDialog
    Dim path As String
    Dim file As String
      
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    ' open FileDialog and get folder path selected by user
    If fd.Show = -1 Then
        path = fd.SelectedItems(1)
    End If
    ' update txtFolder
    txtFolder.Text = path
    ' fill list of excel file into the ListViewFiles
    file = Dir(path & "\*.xls")
    ListViewFiles.ListItems.Clear
    Do While Len(file) > 0
        ListViewFiles.ListItems.Add , , file
        file = Dir()
    Loop
End Sub

Private Sub cmdCancel_Click()
    UserForm_Terminate
End Sub

Private Sub cmdPrint_Click()
    Dim folder As String
    Dim wkb As Workbook
    
    folder = txtFolder.Text
    If folder = "" Then
        MsgBox "Please chose an excel folder."
    Else
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        If ListViewFiles.ListItems.Count > 0 Then
            Dim item As Object
            ' loop through all files and print them
            For Each item In ListViewFiles.ListItems
                If item.Checked = True Then
                    ' open excel file
                    Set wkb = Workbooks.Open(Filename:=folder & "\" & item.Text)
                    ' select the first sheet of the workbook and print it
                    ActiveWorkbook.Sheets(1).Select
                    ActiveWindow.SelectedSheets.PrintOut copies:=1
                    wkb.Close False
'                    Debug.Print folder & "\" & item.Text
                End If
            Next
        End If
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
End Sub

Private Sub UserForm_Terminate()
    Unload Me
End Sub

Giải thích mã :
- Hàm cmdBrowse_Click được gọi khi ta nhấn nút Browse : mở hộp thoại thư mục. Khi một thư mục được chọn, các tập tin excel trong đó sẽ xuất hiện trong khung hiển thị danh sách.
- Hàm chbSelectAll_Click cho phép chọn hoặc không chọn toàn bộ các tập tin trong khung hiển thị danh sách khi ta nhấn hộp chọn "Select all"
- Hàm cmdPrint_Click để in các tập tin excel được chọn khi ta nhấn nút Print.

Không có nhận xét nào:

Đăng nhận xét