Xin hỏi đoạn code dưới đây cần thay đổi (bổ sung) chỗ nào để sau khi thoat khỏi form thì EXCEL cũng thoát luôn và trường hợp đóng form thì EXCEL mở ra ?
Mình vận dụng đoạn code này nhưng khi thoát khỏi form bằng cách bấm nút X (close), sau đó khởi động lại EXCEL thì nó báo lỗi: "Cannot use object linking and embedding" . Kiểm tra thì thấy sau khi thoát form thì EXCEL vẫn thương trú trong Task Manager.
Cám ơn !
Code:
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_STYLE As Long = (-16)
Public Const WS_MAXIMIZEBOX As Long = &H10000
Public Const WS_MINIMIZEBOX = &H20000
Public Sub MinMax(sCaption As String)
Dim hWndForm As Long
Dim iStyle As Long
If Val(Application.Version) < 9 Then
hWndForm = FindWindow("ThunderXFrame", sCaption) 'XL97
Else
hWndForm = FindWindow("ThunderDFrame", sCaption) 'XL2000
End If
iStyle = GetWindowLong(hWndForm, GWL_STYLE)
iStyle = iStyle Or WS_MAXIMIZEBOX
iStyle = iStyle Or WS_MINIMIZEBOX
SetWindowLong hWndForm, GWL_STYLE, iStyle
End Sub
Private Sub UserForm_Initialize()
Call MinMax(MenuChinh.Caption)
SendKeys "%{ }X"
Application.Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
End Sub
Sub UserForm_Resize()
Application.ScreenUpdating = False
Me.Caption = Height
If Me.Height < 100 Then
Application.Visible = False
Else
SendKeys "%{ }X"
Application.Visible = True
End If
MinMax (Me.Caption)
Application.ScreenUpdating = True
End Sub
'-------------------
Option Explicit
Private Sub Workbook_Open()
MenuChinh.Show
Application.Visible = False
End Sub
Mình vận dụng đoạn code này nhưng khi thoát khỏi form bằng cách bấm nút X (close), sau đó khởi động lại EXCEL thì nó báo lỗi: "Cannot use object linking and embedding" . Kiểm tra thì thấy sau khi thoát form thì EXCEL vẫn thương trú trong Task Manager.
Cám ơn !
Code:
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_STYLE As Long = (-16)
Public Const WS_MAXIMIZEBOX As Long = &H10000
Public Const WS_MINIMIZEBOX = &H20000
Public Sub MinMax(sCaption As String)
Dim hWndForm As Long
Dim iStyle As Long
If Val(Application.Version) < 9 Then
hWndForm = FindWindow("ThunderXFrame", sCaption) 'XL97
Else
hWndForm = FindWindow("ThunderDFrame", sCaption) 'XL2000
End If
iStyle = GetWindowLong(hWndForm, GWL_STYLE)
iStyle = iStyle Or WS_MAXIMIZEBOX
iStyle = iStyle Or WS_MINIMIZEBOX
SetWindowLong hWndForm, GWL_STYLE, iStyle
End Sub
Private Sub UserForm_Initialize()
Call MinMax(MenuChinh.Caption)
SendKeys "%{ }X"
Application.Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
End Sub
Sub UserForm_Resize()
Application.ScreenUpdating = False
Me.Caption = Height
If Me.Height < 100 Then
Application.Visible = False
Else
SendKeys "%{ }X"
Application.Visible = True
End If
MinMax (Me.Caption)
Application.ScreenUpdating = True
End Sub
'-------------------
Option Explicit
Private Sub Workbook_Open()
MenuChinh.Show
Application.Visible = False
End Sub
Lần chỉnh sửa cuối: