VBA Terminate Application - Close only open file

BarriCA

New Member
Joined
May 18, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Im using VBA to launch a separate windows program based on the values of a couple of cells.
The program is for some label printing software that calls up a label template based on the user scanning a QR code that populates the cell values that pick the correct label from a folder full of templates in explorer.

Everything works OK, but I have to be able to close the old label template before I scan the new code and open the new label to avoid the wrong label bring printed.
Currently I'm using a "terminate app" code which I run before using a Shex.Open (tgtfile) command to re-launch the labelling software. This closes down the whole labelling application for the Shex.Open command to then open it again.

The problem is its pretty slow, as the whole application effectively restarts every time.

I was wondering if anyone knew a way that I could just close the active label but leave the labelling software itself open, to speed up the loading times.

Appreicate any help.

Thanks

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 = "NiceLabelDesigner.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
Next

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

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top