Tiếp theo bài viết Thêm logo vào userform trong excel VBA một trường hợp đặt ra là khi di chuyển file hay chia sẻ file này cho người khác mà quyên không đính kèm file logo thì người được chia sẻ kia có nhìn thấy logo của bạn không, câu trả lời chắc chắn là không. Vậy làm sao để không cần gửi kèm theo file logo mà người file vẫn hiện logo? ở bài viết này sẽ giải quyết điều đó.

Cách làm như sau:

Chọn một chỗ "trống" trên userform sau đó đặt 1 Image1 và thu nhỏ Image1 cho gọn tiếp theo trong cửa sổ Properties chọn thuộc tính Picture và nhấn vào nút ba chấm "..." bên cạnh trường Picture để duyệt tới và chọn tập tin logo.ico để gán ICO vào Image1 tiếp theo chỉ cần thiết lập thuộc tính Visible (của Image1) thành False để ẩn Image1 khi hiện form lên. Lúc này thì toàn bộ code trong UserForm là:

Option Explicit

Private Const WM_SETICON = &H80
   
#If VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
#Else
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If

Private Sub UserForm_Initialize()
#If VBA7 Then
    Dim lnghWnd As LongPtr
#Else
    Dim lnghWnd As Long
#End If

    If Val(Application.Version) < 9 Then
        lnghWnd = FindWindow("ThunderXFrame", Caption)  'XL97
    Else
        lnghWnd = FindWindow("ThunderDFrame", Caption)  'XL2000
    End If

    SendMessage lnghWnd, WM_SETICON, 0, Image1.Picture.Handle
                                   End Sub