Questions? Search our site or call us at 1-800-722-6004

Opening and Closing an Application from VBA

This example should work with any VBA program such as Excel, Access & Word

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 installation on our Windows 7 machine. 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}!\\.\root\cimv2")
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

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

End Sub

Categories: Tutorial / How-To, Macro / Code Sample, Data Collection, Serial (RS232), BC-Wedge, COM-File, Network (TCP/IP), TCP-Com, TCP-File

Last Updated: 2013.08.30

Need more help?

Don't hesitate to call or email us with your questions

Our office is open 9AM - 5PM Monday Through Friday (E.S.T.)

Technical Support: 215-496-0202

Toll-Free: 1 (800) 722-6004
Skype: taltech1 (Voice only)