关闭所有VBE窗口(MS Access,VB for Aplications) [英] Close all VBE windows (MS Access, VB for Aplications)

查看:47
本文介绍了关闭所有VBE窗口(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆