Share via

VBA to close an open program

Anonymous
2015-01-29T00:12:08+00:00

I looking for some vba code that will close another open program, specifically internet explorer.  

Thanks,

Mike

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Steve Rindsberg 99,166 Reputation points MVP Volunteer Moderator
2015-01-29T17:19:07+00:00

Because of the use of Integers (rather than Longs) and User instead of User32, I'm guessing that you've found some very old (and no longer workable) VB.

I've managed to modify it to the point where it'll compile and run but it takes PPT down more reliably than Notepad.  ;-(

Give this one a try instead (substitute the exe name of the program you want to whack in place of notepad.exe):

Private Sub Command1_Click()

    TerminateProcess ("notepad.exe")

End Sub

Private Sub TerminateProcess(app_exe As String)

    Dim Process As Object

    For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")

        Process.Terminate

    Next

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Steve Rindsberg 99,166 Reputation points MVP Volunteer Moderator
    2015-01-29T20:18:53+00:00

    My pleasure.  Two new additions to my bag 'o tricks too: 

    How to terminate a program, and

    How to terminate PowerPoint.  ;-)

    There may be a more modern way of invoking the other routine, which might be preferable in some cases, since it sends a message to the app, which may then pop up a warning to the user asking if they want to save their work first.

    The way we've got working just tells the other program "Die.  Now."  As long as you know there'll be no data loss to the user, it's all in good fun.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-01-29T18:46:37+00:00

    Nice!   Works perfectly, THANKS!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-01-29T13:39:59+00:00

    Thanks for the tip Steve.  I found the following code, but I get an error with the line in bold.  Also, I probably didn't provide enough information in my original post, but I want to be able to run this code from within PowerPoint.  In the example below they are attempting to close "Notepad", however I want to close "Internet Explorer".  I would need to change that line, but don't know how to reference correctly.

    Any thoughts or help would be appreciated.

    Private Declare Function FindWindow Lib "User" (ByVal lpClassName _

          As Any, ByVal lpWindowName As Any) As Integer

    Private Declare Function PostMessage Lib "User" (ByVal hWnd _

          As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _

          lParam As Any) As Integer

    Private Const WM_QUIT = &H12

    Private Sub CommandButton1_Click()

    Dim sTitle As String

         Dim iHwnd As Integer

         Dim ihTask As Integer

         Dim iReturn As Integer

         sTitle = "Notepad - (Untitled)"

         iHwnd = FindWindow(0&, sTitle)

         iReturn = PostMessage(iHwnd, WM_QUIT, 0, 0&)

         MsgBox "Notepad has been Closed Down"

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Steve Rindsberg 99,166 Reputation points MVP Volunteer Moderator
    2015-01-29T03:22:15+00:00

    Google "vb terminate program" (without the quotes) for some leads.

    Was this answer helpful?

    0 comments No comments