0

I have a file excel and I ' d like that if the user go inside VBA Editor, he can see it but without the windows inside it opened (as default)

I have used the code below but when I run macro nothing happens and, when I go to VBA Editor I continue to see opened the windows ... I don' t understand why ...

Thanks in advance !!!

Sub cc()

Dim wk, CodeWindow

For Each wk In Application.Workbooks
    If Not wk.Name = ThisWorkbook.Name Then
       For Each CodeWindow In wk.VBProject.VBE.Windows
           If CodeWindow.Visible = True Then CodeWindow.Visible = False
       Next CodeWindow
    End If
Next wk

End Sub 

Thanks a lot in advance !!

4
  • So you want all the open Workbooks to be closed? or you want to hide windows? Commented Apr 6, 2020 at 13:34
  • @TsiriniainaRakotonirina it is about the VBA-Editor code windows, not the worksheets. Commented Apr 6, 2020 at 13:41
  • The code will probably only work if the VBA Editor is open before you run it, because CodeWindow.Visible is 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 removeing If CodeWindow.Visible = True Then helps so CodeWindow.Visible = False will run in any case. That might work but I guess not (at least worth a try). Commented Apr 6, 2020 at 13:45
  • 2
    If you just want no VBA windows to be visible by default, close all VBA windows before you save the file (inside the VBA editor, don't just close the VBA editor itself). No code necessary. Excel remembers what code windows are opened when the file is saved. Commented Apr 6, 2020 at 13:47

3 Answers 3

2

This , I think, is the Function you need to Close automatically all the open VBE Windows:

Public Sub CloseAllVBEWindows()
Dim wk As Workbook
Dim CodeWindow As Variant

'If VBE is closed the user shall allow it to open first
If Application.VBE.MainWindow.Visible = False Then
    If MsgBox("VBE is still closed!" & vbCrLf & "The operation requires VBE to be open during its process. Would you like to open it?", vbInformation + vbYesNo) = vbYes Then
        Application.VBE.MainWindow.Visible = True
    Else
        Exit Sub
    End If
End If

'Then it will close all the windows
'Except the Default VBE Windows (Immediate Windows, Locals Windows ...)
For Each wk In Application.Workbooks
    If wk.Name = ThisWorkbook.Name Then
       For Each CodeWindow In wk.VBProject.VBE.Windows
           If CodeWindow.Visible = True And CodeWindow.Type = 0 Then CodeWindow.Visible = False
       Next CodeWindow
    End If
Next wk

End Sub

You need to understand that in VBE even all the Panels are listed, so you shall close only the CodeWindow.Type = 0 (Visual Basic Script Windows).

And it shall solve your issue! Just let me know

Update: If VBE is closed the it will open VBE first and proceed to the Closing

Sign up to request clarification or add additional context in comments.

2 Comments

The OP said "when I run macro nothing happens". So if nothing happens that wasn't the issue. But anyway it is correct that he should check on CodeWindow.Type = 0. But I believe that hiding the windows only works if the editor is already open, because if it is closed none of the windows is .Visible = True therefore the If is never true and therefore none of the windows is ever set to .Visible = False.
@il_betto, I just updated the script to open the VBE first before proceeding. I agree with #PEH that the reason your script didn't work was may be VBE wasn't open. Thanks PEH
1

Thanks a lot, a lot for the help !!

The macro, assembled like this, for me is good !!

Public Sub CloseAllVBEWindows()

Dim wk As Workbook
Dim CodeWindow As Variant

Application.VBE.MainWindow.Visible = True

For Each wk In Application.Workbooks
    If wk.Name = ThisWorkbook.Name Then
       For Each CodeWindow In wk.VBProject.VBE.Windows
           If CodeWindow.Visible = True Then CodeWindow.Visible = False
       Next CodeWindow
    End If
Next wk

End Sub

Fantastic !!

My problem was that as Option of Excel I have no put flag inside the last line of the image attached: "trust access to the VBA project object model"enter image description here

Is there a way, with VBA code, to flag in automatic this Excel option ?

Thanks a lot again !!

1 Comment

No that is of course not possible, setting this trusting flag by code would be a security break as you can imagine. Any (malicious) code could extend its previleges to be trusted then.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.