Saturday, November 6, 2010

Closing a Microsoft Access 2007 Session with Yet Even More Brute Force! (part 2)

My post back in October regarding how to close MsAccess with Brute Force (Part 1) left out a few other tricks... I'll admit this is a freaky thing to deal with to say the least so here are a few BONUS functions that can help you deal with shutting down a Microsoft Access 2007 Interop session programmatically:..

The KillMsAccessApps is a function that iterates through all open processes running on your machine and kills the ones that are named MSACCESS... quite effective indeed.


''' <summary>
    ''' Close and destroy all MSACCESS Applications
    ''' </summary>
    ''' <remarks></remarks>
    Sub KillMsAccessApps()
        For Each p As Process In Process.GetProcessesByName("MSACCESS")
            p.Kill()
        Next p
    End Sub

You may also need to minimize a session of Microsoft Access 2007 from time to time. Since I'm in such a good mood today, I'll throw in these three BONUS snips for minimizing an MSACCE.


''' <summary>
    ''' Minimize all MSACCESS Applications
    ''' </summary>
    ''' <remarks></remarks>
    Sub MinimizeMsAccessApps()
        For Each p As Process In Process.GetProcessesByName("MSACCESS")
            ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_SHOWMINIMIZED)
        Next p
    End Sub

    ''' <summary>
    ''' For minimizing applications
    ''' </summary>
    ''' <param name="hWnd"></param>
    ''' <param name="nCmdShow"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean

    ''' <summary>
    ''' Human readable flag enums for minimizing applications
    ''' </summary>
    ''' <remarks></remarks>
    <Flags()> _
    Private Enum SHOW_WINDOW As Integer
        SW_HIDE = 0
        SW_SHOWNORMAL = 1
        SW_NORMAL = 1
        SW_SHOWMINIMIZED = 2
        SW_SHOWMAXIMIZED = 3
        SW_MAXIMIZE = 3
        SW_SHOWNOACTIVATE = 4
        SW_SHOW = 5
        SW_MINIMIZE = 6
        SW_SHOWMINNOACTIVE = 7
        SW_SHOWNA = 8
        SW_RESTORE = 9
        SW_SHOWDEFAULT = 10
        SW_FORCEMINIMIZE = 11
        SW_MAX = 11
    End Enum