Intermittent Error

Intermittent Error

Postby Jeff Barnes » Wed Aug 18, 2010 7:35 pm

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 view

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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Intermittent Error

Postby StefanHaupt » Thu Aug 19, 2010 7:58 am

Jeff,

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

Code: Select all  Expand view
TRY
    oLoc := CreateObject( "wbemScripting.SwbemLocator" )
  CATCH oErr
    TRY
       oLoc := CreateObject( "wbemScripting.SwbemLocator" )
    CATCH oErr
        oLoc := nil
    END
END
 
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Intermittent Error

Postby Daniel Garcia-Gil » Thu Aug 19, 2010 11:53 am

Jeff

what do you use, Harbour, xHarbour?
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita

Re: Intermittent Error

Postby Jeff Barnes » Thu Aug 19, 2010 11:59 am

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
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Intermittent Error

Postby Daniel Garcia-Gil » Thu Aug 19, 2010 12:02 pm

try
Code: Select all  Expand view

#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  
 
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita

Re: Intermittent Error

Postby Jeff Barnes » Thu Aug 19, 2010 12:28 pm

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
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Intermittent Error

Postby Daniel Garcia-Gil » Thu Aug 19, 2010 12:47 pm

Jeff

Code: Select all  Expand view

#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  
 
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 60 guests