Page 1 of 1

SaveAs Excel file and wxAutomationObject

Posted: Mon Mar 30, 2009 8:22 am
by totobond
Hi all

Here is a piece of code that expects to save a file as Excel format using wxAutomationObject.
When I launch the code (via menu), I receive a message box saying the following "Cannot start an instance of this class".

Code: Select all

wxAutomationObject ExcelObject;
wxString FileName = "test.xls";
ExcelObject.GetInstance(_T("Excel.Application"));
ExcelObject.CreateInstance("Excel.Application");
ExcelObject.PutProperty("Visible", true);
ExcelObject.CallMethod("Workbooks.Add");
ExcelObject.PutProperty("ActiveCell.Font.Bold", true);
ExcelObject.PutProperty("ActiveCell.Value", "test");
ExcelObject.PutProperty("DisplayAlerts", false);
ExcelObject.CallMethod("Workbooks.SaveAs", FileName); 
ExcelObject.CallMethod("Workbooks.Close", true); 
ExcelObject.CallMethod("Quit"); 
I really don't know how to avoid this.
Does anyone have an idea ???

thanks a lot in advance

Antonio

Code: Select all

wxAutomationObject ExcelObject;
/*wxString test("Range(\"B5\").Activate");
//ExcelObject.GetInstance(_T("Excel.Application"));
ExcelObject.CreateInstance("Excel.Application");
ExcelObject.PutProperty("Visible", true);
//ExcelObject.CallMethod("Workbooks.Open", FileName); 
ExcelObject.CallMethod("Workbooks.Add");
ExcelObject.PutProperty("ActiveCell.Font.Bold", true);
ExcelObject.PutProperty("ActiveCell.Value", "test");
ExcelObject.PutProperty("ActiveCell.Value", "tata");
ExcelObject.PutProperty("DisplayAlerts", false);
ExcelObject.CallMethod("Workbooks.SaveAs", FileName); 
ExcelObject.CallMethod("Workbooks.Close", true); 
//ExcelObject.CallMethod("Quit"); 
*/

Posted: Mon Mar 30, 2009 5:41 pm
by timg
It might help to track down the error if you test what the calls return. GetInstance and CreateInstance should return true or false.

By the way, why are you calling both GetInstance and CreateInstance? GetInstance should be used when Excel is already running. If it's not, you should call CreateInstance.

You should not call both.

Posted: Tue Mar 31, 2009 7:36 am
by totobond
you're right.
In fact, I really don't need both methods but anyway it should work.

I solved my problem.
I've installed/built the last wxwidgets release 2.8.10 and now without any changes it works. Seems that 2.8.9 (my previous wxwidgets version) was containing some bugs.

thanks

Antonio

Posted: Mon Aug 03, 2009 6:38 am
by samsam598
totobond wrote:you're right.
In fact, I really don't need both methods but anyway it should work.

I solved my problem.
I've installed/built the last wxwidgets release 2.8.10 and now without any changes it works. Seems that 2.8.9 (my previous wxwidgets version) was containing some bugs.

thanks

Antonio
I tested the above code but there is no new file test.xls created.Can anybody figure me out why?

Regards,
Sam

Posted: Tue Sep 29, 2009 4:41 am
by asbo
I am also trying to use wxAutomation with excel and everything works except the SaveAs method. Like SamSam598, no file gets created.

Does anyone have any thoughts. ta

Posted: Tue Sep 29, 2009 4:49 pm
by timg
I haven't tried it, but it seems like the line:

Code: Select all

ExcelObject.CallMethod("Workbooks.SaveAs", FileName); 
should be:

Code: Select all

ExcelObject.CallMethod("ActiveWorkbook.SaveAs", FileName); 

Posted: Tue Sep 29, 2009 11:47 pm
by asbo
Your a star! I have been doing this while on nightshift and no matter how many times I looked at the msdn website I didn't spot the subtle difference!