Intermittent Error

Post Reply
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Intermittent Error

Post by Jeff Barnes »

Hi,

I have a routine that will check to see if MS-Excel is running and if so, terminate it.

It works about 95% of the time but I get an intermittent error pop up every now and then.

Here is the code I use:

Code: Select all | Expand


FUNCTION WMIService()
   static oWMI
   local oLoc
   if oWMI == nil
      oLoc  := CREATEOBJECT( "wbemScripting.SwbemLocator" )
      oWMI  := oLoc:ConnectServer()
   endif
return oWMI

FUNCTION KillExcel()
   local oWmi, oList, oProc
   IF UPPER(ALLTRIM( cWinVersion() )) = "VISTA"
      Return Nil
   ENDIF
   Syswait(.01)
   oWmi     := WmiService()
   oList    := oWmi:ExecQuery( "select * from Win32_Process" )
   for each oProc in oList
         if oProc:Name = "EXCEL.EXE"
            oProc:Terminate()
            Syswait(1)
         endif
   next
return nil
 


The error that pops up is:

Error description: Error wbemScripting.SwbemLocator/16389 E_FAIL: CONNECTSERVER
Args:

Stack Calls
===========
Called from: win32ole.prg => TOLEAUTO:CONNECTSERVER(0)
Called from: EzSat.prg => WMISERVICE(20820)
Called from: EzSat.prg => KILLEXCEL(20831)


Anyone have any ideas as to why I get this error?
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
StefanHaupt
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Intermittent Error

Post by StefanHaupt »

Jeff,

you can use TRY..CATCH to avoid the error message

Code: Select all | Expand

TRY
    oLoc := CreateObject( "wbemScripting.SwbemLocator" )
  CATCH oErr
    TRY
       oLoc := CreateObject( "wbemScripting.SwbemLocator" )
    CATCH oErr
        oLoc := nil
    END
END
 
kind regards
Stefan
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: Intermittent Error

Post by Daniel Garcia-Gil »

Jeff

what do you use, Harbour, xHarbour?
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Re: Intermittent Error

Post by Jeff Barnes »

Thanks Stefan, I will try your idea to see if it helps :)

Daniel, I use xHarbour.
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: Intermittent Error

Post by Daniel Garcia-Gil »

try

Code: Select all | Expand


#include "fivewin.ch"

function Main

   LOCAL oWnd, oTimer
   
   define window oWnd
   
   define timer oTimer interval 5000 action VerifyExcel( oTimer, oWnd ) of oWnd
   
   activate window oWnd on init oTimer:activate()


return nil

function VerifyExcel( oTimer, oWnd )
   
   local oExcel, oError

   TRY
      oExcel   = GetActiveObject( "Excel.Application" )
      oExcel:Quit()
   CATCH oError
   END  
   
return nil  
 
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Re: Intermittent Error

Post by Jeff Barnes »

Thanks Daniel,

This works if only one instance of Excel is open but what if there are more than one?
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: Intermittent Error

Post by Daniel Garcia-Gil »

Jeff

Code: Select all | Expand


#include "fivewin.ch"

function Main

   LOCAL oWnd, oTimer
   
   define window oWnd
   
   define timer oTimer interval 100 action VerifyExcel( oTimer, oWnd ) of oWnd
   
   activate window oWnd on init oTimer:activate()


return nil

function VerifyExcel( oTimer, oWnd )
   
   local oExcel, oError,  lExit := .F.
   
   TRY
      oExcel   = GetActiveObject( "Excel.Application" )
      oExcel:Quit()
      oWnd:cTitle = time()
      Sleep( 1000 )
   CATCH oError
   END  
   
return nil  
 
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Post Reply