Board index » cppbuilder » Forcfully close Excel

Forcfully close Excel


2006-10-10 10:54:40 PM
cppbuilder16
Hi there I have a class that writes to an excel file, the only problem is
that it will throw an exception when I wrtie to an excel file that is
currently open. Is there anyway I can forcefully close the file. I have the
following code:
FileHandler::FileHandler(String filename)
{
fileXls = filename;
/*Initialise File*/
XLApp = Variant::CreateObject("excel.application"); //open excel
application
XLApp.OlePropertySet("visible",false); //set application to invisible
v0 = XLApp.OlePropertyGet("workbooks"); //get workbook
v0.OleProcedure("Add"); //add one workbook
v1 = v0.OlePropertyGet("Item",1); //select workbook number 1
v2 = v1.OlePropertyGet("WorkSheets"); //Get worksheet
v2.OleFunction("Add"); // add one work sheet
v3 = v2.OlePropertyGet("Item",1); //select worksheet number 1
v3.OlePropertySet("Name","Example"); //rename worksheet it to "example1?
}
FileHandler::~FileHandler()
{
//Close the work book
v1.OleProcedure("Close");
//quite the excel application
XLApp.OleProcedure("Quit");
//unassign variable
XLApp=Unassigned;
}
void FileHandler::Write(double val,int row,int col)
{
char temp[10];
sprintf(temp,"%lf",val);
/*Write val to row, col position in excel*/
v3.OlePropertyGet("Cells").OlePropertyGet("Item",row,col).OlePropertySet("Value",temp);
//this will stop asking you where you want to save your excel file or not
XLApp.OlePropertySet("DisplayAlerts",false);
//save your excel file at "d" and name it as "case1.xls"
XLApp.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs","C:\\TEST1.xls");
}
Any help would be greatly appreciated.
Thanks Mike
 
 

Re:Forcfully close Excel

Hello Michael,
Please see inline.
"Michael" < XXXX@XXXXX.COM >写入消息新闻: XXXX@XXXXX.COM ...
Quote
Hi there I have a class that writes to an excel file, the only problem is
that it will throw an exception when I wrtie to an excel file that is
currently open.
I am not clear that the excel file is opened by your process or another
process.
if opened by another process, it's impossible to close the file. it should
raise an exception
to tell user that it cannot continue.
if opened by yourself process, what is the error msg?
normally, it should not error.
Quote
Is there anyway I can forcefully close the file. I have the following
code:

FileHandler::FileHandler(String filename)
{
fileXls = filename;
/*Initialise File*/
XLApp = Variant::CreateObject("excel.application"); //open excel
application
XLApp.OlePropertySet("visible",false); //set application to invisible
v0 = XLApp.OlePropertyGet("workbooks"); //get workbook
v0.OleProcedure("Add"); //add one workbook
v1 = v0.OlePropertyGet("Item",1); //select workbook number 1
v2 = v1.OlePropertyGet("WorkSheets"); //Get worksheet
v2.OleFunction("Add"); // add one work sheet
v3 = v2.OlePropertyGet("Item",1); //select worksheet number 1
v3.OlePropertySet("Name","Example"); //rename worksheet it to "example1?

}

FileHandler::~FileHandler()
{
//Close the work book
v1.OleProcedure("Close");
//quite the excel application
XLApp.OleProcedure("Quit");
//unassign variable
XLApp=Unassigned;
}

void FileHandler::Write(double val,int row,int col)
{

char temp[10];
sprintf(temp,"%lf",val);
/*Write val to row, col position in excel*/

v3.OlePropertyGet("Cells").OlePropertyGet("Item",row,col).OlePropertySet("Value",temp);
//this will stop asking you where you want to save your excel file or not
XLApp.OlePropertySet("DisplayAlerts",false);
//save your excel file at "d" and name it as "case1.xls"

XLApp.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs","C:\\TEST1.xls");
}

Any help would be greatly appreciated.
Thanks Mike

 

Re:Forcfully close Excel

Its more of the case if the user is viewing the excel file and has forgot to
close it that it throws a "EOleSysError". Is there another way I should go
about handling this?
"chenzero" < XXXX@XXXXX.COM >wrote in message
Quote
Hello Michael,
Please see inline.


"Michael" < XXXX@XXXXX.COM >
写入消息新闻: XXXX@XXXXX.COM ...
>Hi there I have a class that writes to an excel file, the only problem is
>that it will throw an exception when I wrtie to an excel file that is
>currently open.

I am not clear that the excel file is opened by your process or another
process.

if opened by another process, it's impossible to close the file. it should
raise an exception
to tell user that it cannot continue.

if opened by yourself process, what is the error msg?
normally, it should not error.


>Is there anyway I can forcefully close the file. I have the following
>code:
>
>FileHandler::FileHandler(String filename)
>{
>fileXls = filename;
>/*Initialise File*/
>XLApp = Variant::CreateObject("excel.application"); //open excel
>application
>XLApp.OlePropertySet("visible",false); //set application to invisible
>v0 = XLApp.OlePropertyGet("workbooks"); //get workbook
>v0.OleProcedure("Add"); //add one workbook
>v1 = v0.OlePropertyGet("Item",1); //select workbook number 1
>v2 = v1.OlePropertyGet("WorkSheets"); //Get worksheet
>v2.OleFunction("Add"); // add one work sheet
>v3 = v2.OlePropertyGet("Item",1); //select worksheet number 1
>v3.OlePropertySet("Name","Example"); //rename worksheet it to
>"example1?
>
>}
>
>FileHandler::~FileHandler()
>{
>//Close the work book
>v1.OleProcedure("Close");
>//quite the excel application
>XLApp.OleProcedure("Quit");
>//unassign variable
>XLApp=Unassigned;
>}
>
>void FileHandler::Write(double val,int row,int col)
>{
>
>char temp[10];
>sprintf(temp,"%lf",val);
>/*Write val to row, col position in excel*/
>
>v3.OlePropertyGet("Cells").OlePropertyGet("Item",row,col).OlePropertySet("Value",temp);
>//this will stop asking you where you want to save your excel file or not
>XLApp.OlePropertySet("DisplayAlerts",false);
>//save your excel file at "d" and name it as "case1.xls"
>
>XLApp.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs","C:\\TEST1.xls");
>}
>
>Any help would be greatly appreciated.
>Thanks Mike
>


 

{smallsort}

Re:Forcfully close Excel

Please direct your browser at info.borland.com/newsgroups/ and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!