关闭所有VBE窗口(MS Access,VB for Aplications) [英] Close all VBE windows (MS Access, VB for Aplications)
问题描述
如果您像我一样,并且要处理大量代码,则可能不愿一一关闭所有打开的VBE代码窗口. VBE中没有可以做到的功能.怎么做?
If you are like me and you work with lots of code you probably hate to close all open VBE code windows one by one. There is no functionality in VBE which would do it. How to do it?
推荐答案
I was looking for a duplicate for this question. I couldn't fine one. So I ended up at Nee a code of API VBA to capture all handles of particular window by caption/Title and answering that as well :D
逻辑是一样的.有了手柄后,只需使用SendMessage
Well the logic is the same. Once you have the handle, simply close the window using SendMessage
尝试一下
Option Explicit
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
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
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060
Public Sub GetWindows()
'~~> Pass Full Name or Partial Name. This is not case sensitive
Debug.Print GetAllWindowHandles("Microsoft Visual Basic")
End Sub
Private Function GetAllWindowHandles(partialName As String)
Dim hWnd As LongPtr, lngRet As Long
Dim strText As String
hWnd = FindWindowEx(0&, 0&, vbNullString, vbNullString)
While hWnd <> 0
strText = String$(100, Chr$(0))
lngRet = GetWindowText(hWnd, strText, 100)
If InStr(1, strText, partialName, vbTextCompare) > 0 Then
'~~> Close the window
SendMessage hWnd, WM_SYSCOMMAND, SC_CLOSE, 0
End If
'~~> Find next window
hWnd = FindWindowEx(0&, hWnd, vbNullString, vbNullString)
Wend
End Function
以上代码绝对有效.屏幕截图如下.打开了3个VBE(一个用于MS Word,一个用于MS Powerpoint,一个用于MS Excel),并且所有三个都已关闭.
The above code absolutely works. Screenshot below. Opened 3 VBEs (One for MS Word. One for MS Powerpoint and One for MS Excel) and all three closed.
这篇关于关闭所有VBE窗口(MS Access,VB for Aplications)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!