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:
.
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
Is Excel Installed?
Moderator: Rathinagiri
Re: Is Excel Installed?
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
BPD
Convert Dream into Reality through HMG
Convert Dream into Reality through HMG
-
- Posts: 271
- Joined: Sat May 18, 2019 2:11 pm
- DBs Used: Visual FoxPro, FoxPro
- Location: United States of America
Re: Is Excel Installed?
Hi bpd2000,
Thank you for your kind help in programatically determining whether or not MS Excel is installed.
Unfortunately CREATEOBJECT() was also unsuccessful:
.
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
Thank you for your kind help in programatically determining whether or not MS Excel is installed.
Unfortunately CREATEOBJECT() was also unsuccessful:
.
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?
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
BPD
Convert Dream into Reality through HMG
Convert Dream into Reality through HMG
-
- Posts: 271
- Joined: Sat May 18, 2019 2:11 pm
- DBs Used: Visual FoxPro, FoxPro
- Location: United States of America
Re: Is Excel Installed?
Hi bpd2000,
I really appreciate your kind guidance in determining whether MS Excel is installed.
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.
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
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.
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?
Hi Red2.
I do it this way:
or simplifying, just checking if Excel is available:
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
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
-
- Posts: 271
- Joined: Sat May 18, 2019 2:11 pm
- DBs Used: Visual FoxPro, FoxPro
- Location: United States of America
Re: Is Excel Installed?
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
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