Alaska Software Inc. - Re: Excel.exe always stay in processes
Username: Password:
AuthorTopic: Re: Excel.exe always stay in processes
Pascal BoivinRe: Excel.exe always stay in processes
on Fri, 19 Oct 2012 20:31:05 +0200
Hi

I'm getting the same error for years, so here is a little (and drastic)
workaround.

PROCEDURE MAIN
LOCAL cFile, oExcel, oBook, pID
	cFile := "C:\Temp\Potato.XLS"

	oExcel := CreateObject("Excel.Application")
	IF Empty( oExcel )
		MsgBox( "Excel is not installed!" )
		RETURN
	ENDIF

	pID := GetProcessIDFromWindow(oExcel:hWnd)

	oExcel:DisplayAlerts := .F.
	oExcel:visible       := .F.

	//Do whatever you want with Excel
	...

	BEGIN SEQUENCE

		oBook:SaveAs(cFile,xlWorkbookNormal)
		oBook:Close(.F., "", .F.)
		oBook := nil

		 Quit Excel
		oExcel:Quit()
		oExcel:Destroy()
		oExcel := nil

		IF pID <> 0
			KillProcess(pID)
		ENDIF

		RunShell("/e,/select," + cFile, "Explorer.exe",.T.)
	RECOVER USING oError
			MsgBox(oError:Description + Chr(13) + Chr(10) + "Operation: " +
oError:Operation,"Error when saving " + cFile )
	END
RETURN


FUNCTION GetProcessIDFromWindow(hWnd)
LOCAL pID := U2Bin(0)

  DllCall("User32.DLL", DLL_STDCALL, "GetWindowThreadProcessId", hWnd,
@pID)
  pID := BIN2U(pID)

RETURN pID

PROCEDURE KillProcess(pID)
  RunShell("/C TASKKILL /F /PID " + AllTrim(Str(pID,10,0)))
RETURN