I think the code below works.
However note:
- that I did see that not every start is successful for reasons not always known
- not all applications can be started with a hidden window
- some applications need time to start and/or stop
- some applications can not be started multiple times in parallel
So it is mandatory to check the result code!
It is possible to extend this function with extra options (directory to use etc, as additional (optional) parameter. But that makes thinks more complex and I do not need that at the moment
Be aware that you need to make this function / the MsAccess or Excel version trusted, otherwise the windows macro security protection will show a popup and the application will be killed,
Sub StartProcessWithWMI(ByRef PID As Integer, ByVal CmdStr As String, ByRef Result As Integer, Optional ByVal HideWindow As Boolean = False)
'>> TO RUN THIS CODE the code / this file MUST be trusted by Windows !! <<
'>> Use e.g. File -> Options -> TrustCenter -> add location of this DB as trusted path
'>> If Result <> '0' the process has not been started !! see below 'ResultCode'
'https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/create-method-in-class-win32-process
'PID can be verified via windows taskmanager tab details
'>> ResultCode <<
'Successful completion(0)
'Access denied(2)
'Insufficient privilege(3)
'Unknown failure(8)
'Path Not found(9)
'Invalid Parameter(21)
'Other (22 4294967295)
Dim process As Object
Dim strComputer As String
Dim objWMIService As Object
Dim objStartup As Object
Dim objConfig As Object
strComputer = "." 'local computer name is specified using '.'
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
If HideWindow Then
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = vbHide
Else
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = vbNormalFocus
End If
Set process = GetObject("winmgmts:Win32_Process")
Result = process.Create(CmdStr, Null, objConfig, PID)
Set process = Nothing
Set objConfig = Nothing
Set objStartup = Nothing
Set objConfig = Nothing
DoEvents
End Sub