Opening and Closing an Application from VBA

Visual Basic for Applications (VBA) is part of all Microsoft Office products, including Excel, Access, Word, and more. It can easily be used to open up any Windows program. The example below is configured for the BC-Wedge. Note that if you are running on a 64-bit machine, your path will be different. Your path will also be different if you installed BC-Wedge in a location other than default or if you are trying to open a program other than BC-Wedge.

 Sub Auto_Open()
    Dim x As Variant
    Dim Path As String

    ' Set the Path variable equal to the path of your program's installation
    Path = "C:Program Files\BC-Wedge\BC-Wedge.exe"

    x = Shell(Path, vbNormalFocus)
End Sub

Closing any application from VBA can be accomplished with the example code below. This can be used for both our applications or any other windows program. We recommend using this method if you are using BC-Wedge, TCP-Com, COM-File or TCP-File. For other applications, such as WinWedge or B-Coder, there is a much easier method facilitated by DDE (Dynamic Data Exchange). Our WinWedge example can be found at the bottom of the Launching WinWedge from an Excel Macro support article.

The code is documented below. To change the program to TCP-Com, TCP-File, COM-File or any other application, simply change the name of the exe.

Sub TerminateApp()
     '---------------------------------------------------------------------------------------
     ' Terminates the exe process specified.
     ' Uses WMI (Windows Management Instrumentation) to query all running processes
     ' then terminates ALL instances of the exe process held in the variable strTerminateThis.
     '---------------------------------------------------------------------------------------

Dim strTerminateThis As String 'The variable to hold the process to terminate 
Dim objWMIcimv2 As Object, objProcess As Object, objList As Object Dim intError As Integer 

'Process to terminate – you could specify and .exe program name here 
strTerminateThis = "bc-wedge.exe" 

'Connect to CIMV2 Namespace and then find the .exe process 
Set objWMIcimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\.rootcimv2") 
Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'") 
For Each objProcess In objList 
    intError = objProcess.Terminate 'Terminates a process and all of its threads. 
                                    'Return value is 0 for success. Any other number is an error. 
    If intError <> 0 Then Exit For 
Next 

'ALL instances of exe (strTerminateThis) have been terminated 
Set objWMIcimv2 = Nothing 
Set objList = Nothing 
Set objProcess = Nothing 
End Sub

Contact Us