Controlling the VBE

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Controlling the VBE

Post by Rudi »

Hi,

I have this code to close all code panes in the VBE.

Code: Select all

Sub CloseCodePanes()
Dim i As Integer
    On Error Resume Next
    For i = Application.VBE.CodePanes.Count To 1 Step -1
        Application.VBE.CodePanes(i).Window.Close
    Next i
End Sub
It's quite erratic and sometimes closes everything and other times leaves one pane/window open. Anyways, I have two questions based on this code:

1. Is it possible to modify it to close all panes, except the pane that contains this macro?
2. Is it possible to have this macro trigger when the VBE opens (like an auto_open macro)?

TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Controlling the VBE

Post by HansV »

(Question from: Close all code windows/panes in VB Editor)

1) Does this work better?

Code: Select all

Sub CloseAllVBEWindows()
    Dim i As Long
    With Application.VBE.Windows
        For i = .Count To 1 Step -1
            With .Item(i)
                If .Type = 0 Then ' 0 = vbext_wt_CodeWindow
                    .Close
                End If
            End With
        Next i
    End With
End Sub
By the way, the free add-in MZTools adds a menu option to close all VBA windows (optionally keeping the active window open).
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Controlling the VBE

Post by HansV »

2) I'm not sure it would be desirable to close all code windows each and every time the Visual Basic Editor is opened. You could create a separate macro to open the VBE and close all code windows:

Code: Select all

Sub StartVBE()
    Application.VBE.MainWindow.Visible = True
    CloseAllVBEWindows
End Sub
If you place both macros in the personal macro workbook Personal.xlsb, they will be available whenever you start Excel.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Controlling the VBE

Post by Rudi »

TX for the link to the source query.
I was going to link the source query back to here if I got a reply...(which I will be doing now :thumbup: )

Interesting suggestion...I'll provide links in the source to this thread for the answers.

Cheers
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

BBC
NewLounger
Posts: 2
Joined: 17 May 2015, 18:42

Re: Controlling the VBE

Post by BBC »

I am the original poster of
Close All code windows/panes in the VB Editor
Thanks for linking me here Rudi, as per I was going to link the source query back to here if I got a reply...(which I will be doing now)
This has been very helpful and informative. I'll probably put the macro in my personal WB with a keyboard shortcut.

It turns out that the window that was being left open was a "form" design window so that probably explains that problem in my case.

I've done some hunting but can't find a list of the windows "types" so I can add the "form" window for closing as well.

Again, appreciate the help
First time to this forum and will definitely be back.
Brian

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Controlling the VBE

Post by Rudi »

Welcome to Eileen's Lounge, Brian....
See you around!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Controlling the VBE

Post by HansV »

Welcome to Eileen's Lounge!

Make sure that you set a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 library in Tools > References in the Visual Basic Editor.
Press F2 to activate the Object Browser.
Type vbext_WindowType in the search box and press Enter.
You'll see a list of the possible window types:
S0547.png
A userform is probably of type vbext_wt_Designer = 1.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Controlling the VBE

Post by LisaGreen »

Hi,

Coming to this late. Here is some code I use...
I think it would be fairly simple to pick up the current window index and skip closing it.

HTH
Lisa

Code: Select all

Sub subccCloseCodePanes()
' PROJECT.
' NO REPORT.
' NO END MESSAGE.
'

Dim olPane As Object
Dim vbcmlCodeModule As VBIDE.CodeModule
Dim vbclComponent As VBIDE.VBComponent
Dim vbplProject  As VBIDE.VBProject
Dim ilPCount As Integer

Do
  ilPCount = Application.VBE.CodePanes.Count
  On Error Resume Next
  Application.VBE.CodePanes.Item(ilPCount).Window.Close
  If Err.Number <> 0 Then
    Exit Do
  End If
Loop

' ***********************************************************************
End Sub

Stupid reformatting!!! One day I'll learn!!

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Controlling the VBE

Post by LisaGreen »

Okay... some experimenting... The open window caption is....
?Application.VBE.ActiveCodePane.Window.Caption

THis will close all the windows bar te current one.
... And I've tidied up the Dims... L-)

HTH
Lisa

Code: Select all

Sub subccCloseOtherCodePanes()
' PROJECT.
' NO REPORT.
' NO END MESSAGE.
'

Dim ilPCount As Integer
Dim slOpenWindowCaption As String

slOpenWindowCaption = Application.VBE.ActiveCodePane.Window.Caption
Do
  ilPCount = Application.VBE.CodePanes.Count
  If ilPCount = 1 Then
    Exit Do
  End If
  If Application.VBE.CodePanes.Item(ilPCount).Window.Caption _
          <> slOpenWindowCaption Then
    On Error Resume Next
    Application.VBE.CodePanes.Item(ilPCount).Window.Close
    If Err.Number <> 0 Then
      Exit Do
    End If
  End If
Loop
On Error GoTo 0

' ***********************************************************************
End Sub


User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Controlling the VBE

Post by Rudi »

TX for those additions Lisa.
These replies are sure to cover all angles now.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

BBC
NewLounger
Posts: 2
Joined: 17 May 2015, 18:42

Re: Controlling the VBE

Post by BBC »

This will definitely do what I was looking to do .. nice to keep the current one open
Thanks all !
Brian

AlexOfRhodes
NewLounger
Posts: 2
Joined: 29 Mar 2022, 08:00

Re: Controlling the VBE

Post by AlexOfRhodes »

this must be 3rd or 4rth revision.. closes all code panes and designer windows from all unprotected open workbooks and addins

Sub CloseCodepanes()
Dim Module As VBComponent
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
For Each Module In wb.VBProject.VBComponents
If Module.Type = vbext_ct_MSForm Then
Module.DesignerWindow.Visible = False
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.DesignerWindow.Visible
Else
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.CodeModule.CodePane.Window.Visible
End If
Next
Next
Dim ad As AddIn
For Each ad In AddIns
Debug.Print ad.Name
If WorkbookIsOpen(ad.Name) = True Then
If ProtectedVBProject(Workbooks(ad.Name)) = False Then
For Each Module In Workbooks(ad.Name).VBProject.VBComponents
If Module.Type = vbext_ct_MSForm Then
Module.DesignerWindow.Visible = False
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.DesignerWindow.Visible
Else
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.CodeModule.CodePane.Window.Visible
End If
Next
End If
End If
Next
Debug.Print "All codepaned hidden"
End Sub


Function ProtectedVBProject(ByVal wb As Workbook) As Boolean
If wb.VBProject.Protection = 1 Then
ProtectedVBProject = True
Else
ProtectedVBProject = False
End If
End Function

Function WorkbookIsOpen(ByVal sWbkName As String) As Boolean
WorkbookIsOpen = False
On Error Resume Next
WorkbookIsOpen = Len(Workbooks(sWbkName).Name) <> 0
On Error GoTo 0
End Function
Last edited by AlexOfRhodes on 31 Mar 2022, 06:01, edited 2 times in total.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Controlling the VBE

Post by HansV »

Welcome to Eileen's Lounge!

The original question was about closing all code panes. The code posted above does close all code module windows, including standard modules, class modules and userform modules, but not the userforms themselves.
The code that you posted - thanks! :thumbup: - closes the userform windows too.

As a variation on the code I posted earlier:

Code: Select all

Sub CloseAllVBEWindows()
    Dim i As Long
    With Application.VBE.Windows
        For i = .Count To 1 Step -1
            With .Item(i)
                If .Type <= 1 Then ' 0 = vbext_wt_CodeWindow, 1 = vbext_wt_Designer
                    .Close
                End If
            End With
        Next i
    End With
End Sub
Best wishes,
Hans

AlexOfRhodes
NewLounger
Posts: 2
Joined: 29 Mar 2022, 08:00

Re: Controlling the VBE

Post by AlexOfRhodes »

I see what you mean. Because i always used my codepanes in fullwindow i hadn't realized till now that the userform designer and userform codepane were separate that way.
HansV wrote:
29 Mar 2022, 10:27
Welcome to Eileen's Lounge!

The original question was about closing all code panes. The code posted above does close all code module windows, including standard modules, class modules and userform modules, but not the userforms themselves.
The code that you posted - thanks! :thumbup: - closes the userform windows too.

As a variation on the code I posted earlier:

Code: Select all

Sub CloseAllVBEWindows()
    Dim i As Long
    With Application.VBE.Windows
        For i = .Count To 1 Step -1
            With .Item(i)
                If .Type <= 1 Then ' 0 = vbext_wt_CodeWindow, 1 = vbext_wt_Designer
                    .Close
                End If
            End With
        Next i
    End With
End Sub