Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
vbewin = False
End Sub
Private Sub Workbook_Open()
'檔案開啟前先關閉VBE視窗(如果VBE視窗已開啟)
If Application.VBE.MainWindow.Visible Then
Application.VBE.CommandBars.FindControl(ID:=752).Execute
End If
vbewin = True
VBEwindow
End Sub
'-----------------------------------------------------------------
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public vbewin As Boolean
Sub VBEwindow()
'直到檔案關閉
Do While vbewin
DoEvents
Call CheckVBE_Event
Loop
End Sub
Sub CheckVBE_Event()
Dim hwnd As Long
Dim WText As String
Dim L As Long: L = 255
WText = String(255, " ")
'取得當前視窗 Hwnd
hwnd = GetActiveWindow
'取得當前視窗類別名稱
L = GetClassName(hwnd, WText, L)
WText = Left(WText, L)
'VBE視窗的類別名稱為:wndclass_desked_gsk
If WText = "wndclass_desked_gsk" Then
MsgBox "已偵測到VBE視窗開啟,VBE視窗將自行關閉"
'關閉VBE視窗
Application.VBE.CommandBars.FindControl(ID:=752).Execute
End If
End Sub