Here is code I use for this purpose.
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hWnd As LongPtr) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As LongPtr) As LongPtr
#Else
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#End If
Private Const GW_HWNDNEXT = 2
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060
'--------------------
'Close the VBE Window
'--------------------
Private Sub CloseVBEWindow()
#If VBA7 Then
Dim hWnd As LongPtr
#Else
Dim hWnd As Long
#End If
Dim ErrNumber As Long
Dim VBEVisible As Boolean
'<Trust Access to the VBA Project Object Model> is enabled
On Error Resume Next
VBEVisible = Application.VBE.MainWindow.Visible
ErrNumber = Err.Number
On Error GoTo 0
'All Ok
If ErrNumber = 0 Then
If VBEVisible Then Application.VBE.MainWindow.Visible = False
Exit Sub
End If
'<Trust Access to the VBA Project Object Model> is NOT enabled
hWnd = GetVBEWindow
If Not hWnd = 0 Then
Call SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0)
End If
End Sub
'-------------------------
'Get the VBE Window Handle
'-------------------------
#If VBA7 Then
Private Function GetVBEWindow() As LongPtr
Dim hWnd As LongPtr
#Else
Private Function GetVBEWindow() As Long
Dim hWnd As Long
#End If
Dim Buffer As String
'
Const VBENameStart = "Microsoft Visual Basic"
Const LenVBENameStart = 22
'Initialisations
hWnd = GetActiveWindow
Do While 1
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
If hWnd = 0 Then Exit Do
If CBool(IsWindowVisible(hWnd)) Then
Buffer = String$(LenVBENameStart, 0)
Call GetWindowText(hWnd, Buffer, LenVBENameStart + 1)
'Partial name match
If Buffer = VBENameStart Then
'Return value
GetVBEWindow = hWnd
Exit Function
End If
End If
Loop
End Function
CodeWindow.Visibleis only true if the Editor is open/visible. Also note that this code will also close the Project Explorer, Immediate Window, Property Window etc in the VBE. • So you can try if removeingIf CodeWindow.Visible = True Thenhelps soCodeWindow.Visible = Falsewill run in any case. That might work but I guess not (at least worth a try).