Page 1 of 1
Is Excel Installed?
Posted: Tue Jul 09, 2019 7:41 pm
by Red2
Hello All,
May I please ask for some help regarding
TOleAuto. I need to programatically test whether or not MS Excel is installed. On my
desktop machine Excel is installed and the line
loExcel := TOleAuto():New( "Excel.Application" ) properly creates an Excel object. No problem.
However, on my
laptop where Excel is
NOT installed this line fails.
Example:
- TOleAuto_Err.PNG (33.32 KiB) Viewed 3188 times
.
If I comment out line (# 49) then the next line, # 50, executes without error. The debugger's "Variables" tab indicates that loServiceManager is a Class
TOLEAUTOobject. (LibreOffice Writer is installed).
My desktop machine is Windows 10 Pro 64-bit and my laptop is Windows 10 Home 32-bit. The HMG version for both is 3.4.4.
Can someone suggest what I am missing or doing wrong?
Thank you, Red2
Re: Is Excel Installed?
Posted: Wed Jul 10, 2019 4:49 am
by bpd2000
Test
Code: Select all
Function Main
If oExcel := CREATEOBJECT( "Excel.Application" ) == NIL
msginfo( "The Microsoft Excel Application is not installed on this Computer!")
return nil
else
msginfo( "The Microsoft Excel Application is installed on this Computer!")
// your next code
endif
RETURN NIL
Re: Is Excel Installed?
Posted: Wed Jul 10, 2019 2:05 pm
by Red2
Hi bpd2000,
Thank you for your kind help in programatically determining
whether or not MS Excel is installed.
Unfortunately
CREATEOBJECT() was also unsuccessful:
- ExcelObj_Err.PNG (21.85 KiB) Viewed 3125 times
.
I also tied this trapping and recovering inside a BEGIN SEQUENCE ... RECOVER ... END. That did not help either.
Question: What am I
doing or
not doing correctly? I simply want to determine whether or not MS Excel is installed?
Again, thank you very much for your help.
Red2
Re: Is Excel Installed?
Posted: Wed Jul 10, 2019 2:25 pm
by bpd2000
Red2 wrote: ↑Wed Jul 10, 2019 2:05 pm
Question: What am I
doing or
not doing correctly? I simply want to determine whether or not MS Excel is installed?
First: try with creating sample app with above code, if it is Ok, then error is in your code
second : I am requesting to provide a sample that can reproduce this error
Re: Is Excel Installed?
Posted: Wed Jul 10, 2019 3:09 pm
by Red2
Hi bpd2000,
I really appreciate your kind guidance in determining whether MS Excel is installed.
- I created a new project
Added my 7 lines of code to MAIN.PRG's function Main
Commented out function Main's automatically generated three "Window" lines.
Set module MAIN.PRG as Main
Removed Main.Fmg from the project
The only code in the entire project are those 7 lines as seen in the image below. This simple code still has the same problem.
- SimpleExample.PNG (36.71 KiB) Viewed 3112 times
Is there better code to find out whether MS Excel is installed? I would be very grateful for any suggestions.
Again, thank you very much, Red2
Re: Is Excel Installed?
Posted: Thu Jul 11, 2019 7:08 am
by edk
Hi Red2.
I do it this way:
Code: Select all
Function Main
Local oExcel, oError, bErrBlck
Local lIsExcel := .F.
// catch any errors
bErrBlck := ErrorBlock( { | oError | ExcelError( oError, lIsExcel ) } )
BEGIN SEQUENCE
oExcel := CREATEObject( "Excel.Application" )
lIsExcel := .T.
(...)
RECOVER USING oError
//part of the execution code after an error, e.g. closing Excel.
(...)
IF lIsExcel
oExcel:DisplayAlerts := .F.
oExcel:WorkBooks:Close()
oExcel:Quit()
ENDIF
END
//Restore the previous ErrorBlock
ErrorBlock( bErrBlck )
(...)
Return Nil
**************************************************************
FUNCTION ExcelError( oError, lIsExcel )
Local cMessage:='Error ', i:=2 , HtmArch, xText
IF !lIsExcel
MsgStop( "The Microsoft Excel Application is not installed on this Computer!" )
BREAK oError
RETURN Nil
ENDIF
// add subsystem name if available
IF VALTYPE( oError:subsystem ) = 'C'
cMessage += oError:subsystem()
ELSE
cMessage += "???"
ENDIF
// add subsystem's error code if available
IF VALTYPE( oError:subCode ) = 'N'
cMessage += "/" + LTRIM( STR( oError:subCode ) )
ELSE
cMessage += "/???"
ENDIF
// add error description if available
IF VALTYPE( oError:Description ) = 'C'
cMessage += CRLF + oError:Description
ENDIF
// add either filename or operation
DO CASE
CASE !Empty( oError:filename )
cMessage += ": " + oError:filename
CASE !Empty( oError:operation )
cMessage += ": " + oError:operation
ENDCASE
HtmArch := Html_ErrorLog()
Html_LineText( HtmArch, '<p class="updated">Date:' + Dtoc (Date() ) + " " + "Time: " + Time() )
Html_LineText( HtmArch, cMessage + "</p>" )
cMessage += CRLF + CRLF
WHILE ! Empty( ProcName( i ) )
xText := "Called from " + ProcName( i ) + "(" + ALLTRIM( STR( ProcLine( i++ ) ) ) + ")" + CRLF
cMessage += xText
Html_LineText( HtmArch, xText )
ENDDO
Html_Line( HtmArch )
MsgStop( cMessage, "Error processing Microsoft Excel spreadsheet." )
BREAK oError
RETURN Nil
or simplifying, just checking if Excel is available:
Code: Select all
Function Main
Local oExcel, oError, bErrBlck
// catch any errors
bErrBlck := ErrorBlock( { | oError | ExcelError( oError ) } )
BEGIN SEQUENCE
oExcel = CREATEObject( "Excel.Application" )
RECOVER
MsgStop( "The Microsoft Excel Application is not installed on this Computer!" )
//Restore the previous ErrorBlock
ErrorBlock( bErrBlck )
RETURN Nil
END
//Restore the previous ErrorBlock
ErrorBlock( bErrBlck )
(...)
Return Nil
**************************************************************
FUNCTION ExcelError( oError )
BREAK oError
RETURN Nil
Re: Is Excel Installed?
Posted: Thu Jul 11, 2019 6:03 pm
by Red2
Hi edk,
Thank you very much for your expert guidance. Your code accurately detects whether MS Excel is installed (and can also detect LibreOffice Calc).
I do not know how to say thank you enough!
You also provided me with a great lesson on HMG's BEGIN SEQUENCE...BREAK...RECOVER...END. I was trying to use it incorrectly. While VFP's TRY...CATCH TO...FINALLY...ENDTRY is generally similar they are clearly not interchangeable. This was valuable instruction for me.
In any case, thanks again!
Red2