0

I have some vbs/vba scripts running from time to time. But I don't want next vbscript to be proceeded while another either vba- or vbscript is running. There is no problem to determine if another vbscript is running and delay execution of current vbscript:

Set objSWbemServices = GetObject ("WinMgmts:Root\Cimv2") 
Set colProcess = objSWbemServices.ExecQuery _ 
("Select * From Win32_Process where name = 'wscript.exe'")
Do While colProcess.Count > 1
    WScript.Sleep 10000
    Set colProcess = objSWbemServices.ExecQuery _ 
    ("Select * From Win32_Process where name = 'wscript.exe'")
Loop

But I can't find a particular process of vba script running in Excel. How one can check in vbscript if vba script is running?

Community
  • 1
  • 1
Vadim Shkaberda
  • 2,807
  • 19
  • 35
  • 1
    Perhaps you could check if Excel is consuming nontrivial CPU and not run the VBScript when that is happening. It doesn't appear to be easy, but you can google for VBScripts that give the %CPU usage that you get in Task Manager. This shoots up for Excel when a VBA script is running. – John Coleman Aug 23 '16 at 13:34
  • Thank you, it's a worth idea. Though, there can be a problem when `Wait` function is used in vba. – Vadim Shkaberda Aug 23 '16 at 14:20
  • 1
    It is admittedly a hackish idea, but sometimes testing for something which is measurable is the only way to get a handle on something which isn't. Another idea -- if you are the author of both the VBScript and the VBA macro -- perhaps you can have the VBA macro raise a flag (in a log file or perhaps the registry) when it starts and again when it is done. The script can then just check the flag. – John Coleman Aug 23 '16 at 14:39
  • @JohnColeman It's an interesting idea too, but that's not my case. – Vadim Shkaberda Aug 23 '16 at 18:41

1 Answers1

1

I don't think that is possible, as far as I know, VBA in Excel is not executed as a separate process in the Windows operating system.

However, it is possible to find the WSH/WScript/CScript process for your vbscript by matching the CommandLine column from Win32_Process with the specific script name.

some1
  • 857
  • 5
  • 11
  • Thank you for your answer. I don't need to find out specific script name. Counting is quite enough in my case (I have a possibility at most 2 scripts executions being overlapped). Matching with Excel filenames is not an option either. – Vadim Shkaberda Aug 23 '16 at 10:53
  • Will Excel even let you execute VBA two at a time? Even assuming you tie the macros to two separate buttons, if you click one, it takes a long time to process and in the mean time you click the second, will it even let you execute that macro? – SandPiper Aug 23 '16 at 13:38
  • @SandPiper The question is about how *VBScript* can check if a *VBA* program is running. There is no problem in having a VBScript script running in a VBScript interpreter at the same time a VBA script is running in Excel. – John Coleman Aug 23 '16 at 13:53
  • Missed that, my mistake. Thank you. – SandPiper Aug 23 '16 at 13:57
  • @SandPiper By the way, yes. You can even have running vba script and initiate execution of the same vba script using vbscript. Obviously, it will be executed as another process. – Vadim Shkaberda Aug 23 '16 at 14:06
  • @SandPiper I haven't used it myself, but it is even possible to use VBScript to emulate multithreading for VBA, as explained in some of the answers to this question: http://stackoverflow.com/q/5721564/4996248 – John Coleman Aug 23 '16 at 15:21