We use open and close method for Excel OLE object. But sometime the object will mapping to wrong AninateBox command.
Example 1, this is OK
Code: Select all
Local hExcelApp := CreateObject("Excel.Application")
Local hWorkBook := hWorkBooks:Open("test")
hWorkBook:Close()
hExcelApp:Quit()
Example 2, NOT OK.
Code: Select all
Local hExcelApp := CreateObject("Excel.Application")
Local hWorkBook := hWorkBooks:Open("test", 0, .t. ) // Open in readonly mode without update external link
hWorkBook:Close(.t. , "test2") // Close and save in test2
hExcelApp:Quit()
Code: Select all
#include <hmg.ch>
Procedure Main()
Local hExcelApp := CreateObject("Excel.Application")
Local hWorkBook
Local hWorkBooks := hExcelApp:WorkBooks
Local bSaveHandler, oError
Local cTemPath1 := GetTempFolder(), cTemPath2
If Right(cTemPath1, 1) <> '\'
cTemPath1 := cTemPath1 + '\'
EndIf
MsgInfo("Temporaray path is: " + cTemPath1)
cTemPath2 := cTemPath1 + "test2"
cTemPath1 := cTemPath1 + "test1"
// Using 3 parameters will cause the HMG select OpenAnimateBox function
// hWorkBook := hWorkBooks:Open(FileName:="D:\test.xls", UpdateLinks:=0, ReadOnly:=True)
// hWorkBook := hWorkBooks:Open("D:\test.xls", 0, .t., 1) // This is OK
// hWorkBook := hWorkBooks:_Open("D:\test.xls", 0, .t.) // Try this also
hWorkBook := hWorkBooks:Add // Add new workbook
hWorkBook:SaveAs(cTemPath1)
hWorkBook:Close() // Close
hWorkBook := hWorkBooks:Open(cTemPath1) // Open again (not problem)
hWorkBook:Close(.f.) // Close without saving (not problem)
// Here will problem. Put error handling.
bSaveHandler := errorblock( { |x| break(x) } )
BEGIN SEQUENCE
hWorkBook := hWorkBooks:Open(cTemPath1, 0, .t. ) // Open again in readonly mode (without update external link)
RECOVER USING oError
* This is the code to execute if the above code fails
MsgStop( oError:Description + " " + oError:Operation , "Report Error:" )
MsgInfo("Try to open again", "Trying")
hWorkBook := hWorkBooks:Open(cTemPath1) // Open again (not problem)
END
errorblock( bSaveHandler )
bSaveHandler := errorblock( { |x| break(x) } )
BEGIN SEQUENCE
hWorkBook:Close(.t. , cTemPath2) // Close and save the workbook in new name
RECOVER USING oError
* This is the code to execute if the above code fails
MsgStop( oError:Description + " " + oError:Operation , "Report Error:" )
END
errorblock( bSaveHandler )
hExcelApp:Quit()
MsgInfo("Click OK to quit", "Finish")
Return Nil
Kek