Lập trình VBA nhúng Zalo vào Excel với BSTaskPane trong BSAC

Liên hệ QC

Nguyễn Duy Tuân

Nghị Hách
Thành viên danh dự
Tham gia
13/6/06
Bài viết
4,649
Được thích
10,138
Giới tính
Nam
Nghề nghiệp
Giáo viên, CEO tại Bluesofts
Đây là ví dụ để đưa Zalo vào Task Pane trong Excel, bạn cũng làm tương tự với các phần mềm khác. CHỉ cần xác định Title hoặc Class, Handle của ứng dụng đang mở trên Desktop.

Mã:
Option Explicit
'Author: Nguyen Duy Tuan - http://bluesofts.net
'Facebook: https://www.facebook.com/groups/hocexcel
Dim TP As BSTaskPane
Sub CreateTpZalo()
    Dim TPs As New BSTaskPanes 'Task Pane list
    'SinkControl:
    '   + Userform
    '   + Handle to window
    '   + Class:Title
    '   + Title
    'Zalo is title of Zalo App
    Set TP = TPs.Add("Task Pane: Zalo", "Zalo", False, dpLeft)
    'Ok!
    Set TPs = Nothing
End Sub

Sub DestroyTpZalo()
    'Dim TPs As New BSTaskPanes
    'TPs.Clear 'Clear all task panes
    'Set TPs = Nothing
    If Not TP Is Nothing Then 'Only Zalo app
        TP.Remove
        Set TP = Nothing
    End If
End Sub

(*) Download file Excel mã nguồn: https://drive.google.com/file/d/1_79x4nazjYy-tT6xNqT5aWABcsxwGNdY/view?usp=sharing
(*) Cập nhật BSAC, hoặc Add-in A-Tools 02-10-2020 tại đây: http://bluesofts.net/giai-phap-phan...-thao-va-quan-tri-du-lieu-excel-qua-mang.html
 
Khoãng 10 năm trước Tuân viết *.OCX TaskPanes Cho Office ===> 10 Năm sau Mạnh mò ra *.OCX TaskPanes ===>Phỏng biết 10 năm sau nữa ai sẻ là người tiếp lửa đam mê đây ????!!!!!!!!!!! -0-0-0- -0-0-0--0-0-0--0-0-0--0-0-0--0-0-0-
 
Khoãng 10 năm trước Tuân viết *.OCX TaskPanes Cho Office ===> 10 Năm sau Mạnh mò ra *.OCX TaskPanes ===>Phỏng biết 10 năm sau nữa ai sẻ là người tiếp lửa đam mê đây ????!!!!!!!!!!! -0-0-0- -0-0-0--0-0-0--0-0-0--0-0-0--0-0-0-

Biết đâu 1 năm sau bạn có gì đó "độc", "dị" "lạ" thì sao nhỉ :D
 
CẬP NHẬT MÃ NGUỒN "NHÚNG ZALO VÀO EXCEL"
Vừa qua có nhiều thành viên viên đã chạy thử ví dụ về nhúng Zalo vào Excel mà tôi đã hướng dẫ và gửi file mã nguồn. Tuy nhiên mọi người vẫn bị lỗi hay chưa khai thác được các thành phần của Task Pane trong #BSAC vì vậy tôi đã cập nhật lại code cho ví dụ này kỹ hơn, qua đó các bạn thấy cách viết code bài bản với Task Pane cũng như khai thác rộng hơn với BSAC.
1. Kiểm tra Zalo đã được mở chưa?
2. Nếu vô tình người dùng bấm vào nút [X] thì phải mở lại Task Pane như thế nào.
3. Nếu ở đâu đó ứng dụng bị lỗi thì biến TP là nothing khi đó Zalo vẫn đang sống trong Task Pane ẩn, làm sao để moi lại nó ra?
...
(*) Hãy đảm bảo update BSAC hoặc Add-in A-Tools muộn nhất ngày 02-10-2020
(*) File mã nguồn cập nhật mới tại đây: https://drive.google.com/file/d/1_79x4nazjYy-tT6xNqT5aWABcsxwGNdY/view?usp=sharing

Mã:
Option Explicit
'Author: Nguyen Duy Tuan - http://bluesofts.net
'Facebook: https://www.facebook.com/groups/hocexcel
#If VBA7 Then
Public Declare PtrSafe Function CountFileInProcess Lib "BSAC.ocx" (ByVal FileName As Variant) As Long
#Else
Public Declare Function CountFileInProcess Lib "BSAC.ocx" (ByVal FileName As Variant) As Long
#End If
Dim TP As BSTaskPane
Sub CreateTpZalo()
    Dim TPs As New BSTaskPanes 'Task Pane list
    Dim MustCreate As Boolean
    Dim Title As String: Title = "Task Pane: Zalo" 'It is the same in sub DestroyTpZalo()
    'SinkControl:
    '   + Userform
    '   + Handle to window
    '   + ClassName:Title (if only find class name then "ClassName:"
    '   + Title
    'In this demo, "Zalo" is title of Zalo App
    'Class and Title of appilcation use "Spy++ tool" or other to find
    If CountFileInProcess("Zalo.exe") = 0 Then
        MsgBoxW "Please run Zalo app.", vbCritical, "Zalo not running"
        Exit Sub
    End If
    If Not TP Is Nothing Then
        MustCreate = TP.Client.SinkControl = 0 'Not success
        If Not MustCreate Then 'Exists, check is it Zalo?
            If TP.Caption <> Title Then
                If MsgBoxW("Do you want to remove TP and recreate it?", _
                        VbMsgBoxStyle.vbQuestion + vbYesNo, _
                        "Create Zalo") = vbYes Then
                    TP.Remove
                    Set TP = Nothing
                    MustCreate = True
                End If
            End If
        End If
    Else
        MustCreate = True
    End If
    If MustCreate Then
        Set TP = TPs.Add(Title, "Zalo", False, dpLeft)
        TP.AllowHide = False 'Prevent user from clicking on "X" of Task Pane
    Else
        TP.Visible = True
    End If
    'Ok!
    Set TPs = Nothing
End Sub

Sub DestroyTpZalo()
    Dim TPs As New BSTaskPanes
    'TPs.Clear 'Clear all task panes
    Dim Title As String: Title = "Task Pane: Zalo"
    Dim Idx As Long
    If TPs.Count > 0 And TP Is Nothing Then 'try to get instance of Task pane if it is lost
        'Variable "TP" may be lost when you have bug anywhere
        Idx = TPs.IndexOf(Title) 'Find TP with Title
        If Idx >= 0 Then
            Set TP = TPs(Title)
        End If
    End If
    If Not TP Is Nothing Then 'Only Zalo app
        TP.Remove
        Set TP = Nothing
    End If
    Set TPs = Nothing
End Sub
 
Web KT
Back
Top Bottom