Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Suggestions please
Sandra Truax 
         
3 years ago
I have you a challenge. I asked ChatGPT to write Access VBA code to close all open programs (pdfs, word, excel, notepad, etc)  EXCEPT Access.  The following is what I was given, but was hoping someone would look at it before I excute it to see if they see any red flags.  

Option Explicit

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Const WM_CLOSE = &H10
Const GW_HWNDNEXT = 2
Const GW_CHILD = 5

Sub CloseOtherPrograms()
    Dim hWnd As Long
    Dim wndTitle As String
    
    ' Get the handle of the Access application window
    Dim accessHWnd As Long
    accessHWnd = FindWindow("OMain", Application.Caption)
    
    ' Loop through all top-level windows
    hWnd = GetWindow(Application.hWndAccessApp, GW_CHILD)
    Do While hWnd <> 0
        ' Skip the Access application window
        If hWnd <> accessHWnd Then
            ' Get the window title
            wndTitle = GetWindowCaption(hWnd)
            ' Close specific programs except Access
            Select Case GetProcessName(wndTitle)
                Case "Acrobat" ' Adobe Acrobat (PDF)
                    PostMessage hWnd, WM_CLOSE, 0&, 0&
                Case "WINWORD" ' Microsoft Word
                    PostMessage hWnd, WM_CLOSE, 0&, 0&
                Case "EXCEL"   ' Microsoft Excel
                    PostMessage hWnd, WM_CLOSE, 0&, 0&
                Case "Notepad" ' Notepad
                    PostMessage hWnd, WM_CLOSE, 0&, 0&
            End Select
        End If
        ' Move to the next window
        hWnd = GetWindow(hWnd, GW_HWNDNEXT)
    Loop
End Sub

Function GetProcessName(windowTitle As String) As String
    Dim processName As String
    processName = Split(windowTitle, " - ")(0)
    GetProcessName = Trim(processName)
End Function

Function GetWindowCaption(hWnd As Long) As String
    Dim captionLength As Long
    Dim caption As String
    captionLength = GetWindowTextLength(hWnd) + 1
    caption = Space(captionLength)
    GetWindowText hWnd, caption, captionLength
    GetWindowCaption = Left(caption, captionLength - 1)
End Function
Richard Rost  @Reply  
          
3 years ago
I don't see any red flags but without going through it line by line it looks okay to me. I use a very similar process in my Watchdog template to shut down just Access if it's not responsive and it started again.
Sandra Truax OP  @Reply  
         
3 years ago
Thanks Richard. I appreciate your taking the time to look at this. Hope you are all healed up from your surgery and hope have a blessed day.
Kevin Yip  @Reply  
     
3 years ago
Hi Sandra, the code won't work because of the line:

     hWnd = GetWindow(Application.hWndAccessApp, GW_CHILD)

which essentially only looks for windows running under Access, i.e. the VBA editor window, the main app window, etc.

To look for windows from other programs, you need to replace the line with:

     hWnd = FindWindow(vbNullString, vbNullString)

The null strings mean no specific window is specified, so you will get all windows returned to you.

But the problem is that there are a TON of programs running at any time -- opened programs, hidden programs, programs that go on and off and can't be interrupted, etc.  So you can't just "close all" programs that aren't Access, because some programs are not supposed to be closed.

To get a list of open programs that you can actually close, I have the best success with Powershell, which is Windows app that is a beefed-up version of the command line app.  The picture below shows a sample Powershell command that returns a list of running programs like Firefox, Access, Excel, etc.  But it STILL shows some hidden processes that we aren't supposed to touch.  With further tweaking, you may be able to make it work.

But the best thing to do is just avoid doing this kind of thing.  In the early days of Windows (the 90s perhaps), things were simple and you could do this more easily.  Nowadays, a ton of things are running at any given time inside Windows.

Instead, do what Richard alluded to in his post: close only SPECIFIC programs.  Close only the programs that have known names, paths, and process IDs.
Kevin Yip  @Reply  
     
3 years ago

Kevin Yip  @Reply  
     
3 years ago
In the above picture, Calculator was not a running program so I don't know why it was there.  NVIDIA Share, SoftwareUpdateNotificationService, SystemSettings, and TextInputHost were all background hidden processes; and they all had "window titles" even though they had no visible windows.  The rest are programs that I actually opened myself.  So even this is not a perfect solution.
Sandra Truax OP  @Reply  
         
3 years ago
Thank you Kevin for the information. I would probably not have been a happy camper if I'd ran the code.  Glad I checked before I tried it.  I came up with a simple solution using Perfect Keyboard that I think I will stick with before I mess something up I can't fix. Again thanks for checking and letting me know.
Richard Rost  @Reply  
          
3 years ago
Disclaimer: yeah, I just gave the code a quick cursory glance. Actually READING through code that I didn't write gives me a migraine. LOL

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 10:06:42 AM. PLT: 1s