Link to home
Create AccountLog in
Microsoft Excel

Microsoft Excel

--

Questions

--

Followers

Top Experts

Avatar of Iver Erling Arva
Iver Erling Arva🇳🇴

Cannot kill Excel background processes from VB Script
Hi! I have run into a somwhat strange - to me at least - situation trying to kill Excel processes from my Windows 10 environment.

The case is this:

I ofte find that Excel leaves running, seemingly empty processes running in the background after I close an Excel workbook. I have a RPA-software (UFT) that uses VBScript and Excel to do its work, so I typically open up an Excel workbook and have the script getting its input data from there. My problem is that when VBS shall access the Excel workbook it typically picks one of the empty background processes, and not the workbook I opened.
I have tried everything I can think of to go throug the processes on the pc and kill the Excel processes, but to no avail. I can find them, no problem, but I cannot seem to be able to kill them off without manually open the Task manager and close them there, one by one until only the correct process, the one listed under Apps and not under Background processes is the only one left.

Can anyone please help me with this?

Thank you very much!

All the best from Iver in Oslo, Norway

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ZeropointNRGZeropointNRG🇦🇺

Where's your code?

Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run "taskkill /im EXCEL.EXE", , True

Open in new window

Try that.

And this is so no cmd window shows.
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run "cmd /c taskkill /im EXCEL.EXE", 0, True

Open in new window


Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Ok, so I use:
Dim oShell
Set oShell = CreateObject("Shell.Application")
oShell.Run "taskkill /im EXCEL.exe", ,True

Open in new window

This triggers the following error:

The object does not support the specified property or method.

What am I doing wrong?

Brgds
IVer

Avatar of ZeropointNRGZeropointNRG🇦🇺

Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run "cmd /c taskkill /im EXCEL.EXE", 0, True

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Ah! Changed to WScript.Shell instead. THat killed the one Excel process I wanted to keep, but none of the background Excel processes. I want to keep the foreground process, but kill the background Excel processes.

IVer

Avatar of ZeropointNRGZeropointNRG🇦🇺

What are the other processes?

Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

The processes all look the same. Microsoft Excel (32-bits) in the task manager.

The only difference is that the shadow processes are in the background, not visible on the taskbar and listed under background processes, and the one I want to keep is a regular opened workbook listed under apps in the task manager.

If I loop through workbooks using GetObject("Excel.Application"), the ones I want to kill have no worksheets, whereas the one I want to keep has, but the myExcel.Quit (excel_Gen in my code) method doesn't work for some reason.
Do
	found = false
	Set excel_Gen = eval("GetObject(,"&CHR(34)&"Excel.Application"&CHR(34)&")")
	sts = Err.Number
	If TypeName(excel_Gen) = "Empty" Then
		msgbox "UFT cannot find Excel. Must abort."
		exittest 'Special UFT command
	End If
	found = true
	cntWb = 0
	Set excel_Sheet = nothing 'Necessary to avoid error message when checking on it later
	If sts = 0 Then
		'Loop through workbooks to try and find the correct worksheet
		For each wb in excel_Gen.workbooks
			cntWb = cntWb + 1
			wb.activate
			For i = 1 to wb.Worksheets.Count
				Set ws = wb.Worksheets(i)
				'The correct sheet should have the following contents in E1 or D1 - Not 100% proof, but close enough
                                'See if I have found the rigth sheet
				If ws.cells(1,5).value = "FORSIKRING" or ws.cells(1,4).value = "FORSIKRING" Then
					Set excel_Sheet = ws
					Exit For
				End If
			Next
			If not (excel_sheet is nothing) Then
				Exit Do 'found the right one. No further search necessary
			End If
		Next
		'Close empty instance and search for next
		excel_Gen.Quit
		Set excel_Gen = Nothing
		count = count + 1
	ElseIf sts <> 429 Then
		Msgbox Err.Number & ": " & Err.description
	End If
	If count > 10 Then
		Msgbox "Cannot stop Excel processes. Must be done manually before I can continnue."
		count = 5
	End If
Loop While found = true

Open in new window

This is able to find the correct Excel Sheet to use, but not able to kill off the other processes. And since it cannot kill them off it keeps on finding the same process over and over.

Sorry for the somewhat messy code which has become this way after a lot of trying and failing.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ZeropointNRGZeropointNRG🇦🇺

But they are separate processes correct? Do they have a PID?

Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Yes, they do, but I don't know which process has what PID as they all look the same in the PID list.

EXCEL.EXE PID 7908 Running User:IVE
EXCEL.EXE PID 22808 Running User:IVE
EXCEL.EXE PID 17264 Running User:IVE

and I cannot see anything there that make it possible to pick which one is the foreground process and which ones are the ones to kill.

IVer

Avatar of ZeropointNRGZeropointNRG🇦🇺

mmm, not sure since PID changes, maybe someone smarter can help you out. I might try this myself see if I can come up with a solution.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

There must be a property/characterisic that is different since one of them shows up under Apps and the others under background processes.
If I only had a way to connect the found PID with the process found with GetObject("Excel.Application") I could use GetObject to find the correct process and taskkill to kill it off.

IVer

Avatar of ZeropointNRGZeropointNRG🇦🇺

I've found out too that the cause of these processes not closing is because when you have a reference to Excel it will not close down.  Removing the reference allows the connection to be released.

You should seperate the open Excels as another problem to solve. Use the VBscript to focus on it's own Excel (with CreateObject).
So the VBscript opens the Excel, and thereby always tracking the correct Excel window, instead of having Excel already open and trying afterwards to get VBscript to get the correct one.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ZeropointNRGZeropointNRG🇦🇺

You might be able to get a general idea what's wrong here with this link.

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ff730962(v=technet.10)

Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

@Kimputer: The problem is that this is a temporary Excel sheet opened from a PL/SQL program. I just want to use the data in it, but to do so I need to find the right Excel process. I don't actually have to close the others, I could do it if only I could loop through them with GetObject("Excel.Application") somehow.

IVer

Avatar of ZeropointNRGZeropointNRG🇦🇺

Yup, what Kimputer said is the logical work around. But solving this problem is more interesting ;P

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of KimputerKimputer

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Ok. I can do that workaround, but I have to keep using VBScript since that is the script language used by the RPA software UFT that I am running here.

Would be nice to solve the original problem though, since I am using Excel a lot, both with UFT, PL/SQL and on its own. And it is leaving this background processes running all the time, also when only using Excel itself. And in that particular case I would consider it to be a bug.

Come to think of it, I could perhaps make a small .exe with VB.Net and just call it from VBScript. THat would be a possibility of course.

Thanks!
IVer

Calling the .exe through the VBScript is indeed very possible, pass along the info you need through the arguments.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Solution 1: For now,  not let PL/SQL open Excel, but save the file. Then the VBscript can pick up the file with it's own Excel.
Solution 2: Make a small .exe with VB.Net and just call it from VBScript.

Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Thank you for your kind help! I have not been able to completely solve my problem, but at least have a good idea about how to go on. I
Brgds
IVer in Oslo

Avatar of Iver Erling ArvaIver Erling Arva🇳🇴

ASKER

Thank you for your kind help! I have not been able to completely solve my problem, but at least have a good idea about how to go on. I
Brgds
IVer in Oslo

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Microsoft Excel

Microsoft Excel

--

Questions

--

Followers

Top Experts

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.