SaveAs Excel file and wxAutomationObject

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
totobond
Earned a small fee
Earned a small fee
Posts: 11
Joined: Mon Oct 02, 2006 10:01 am

SaveAs Excel file and wxAutomationObject

Post 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"); 
*/
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post 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.
totobond
Earned a small fee
Earned a small fee
Posts: 11
Joined: Mon Oct 02, 2006 10:01 am

Post 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
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Post 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
asbo
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Jun 09, 2008 5:12 am

Post 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
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post 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); 
asbo
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Jun 09, 2008 5:12 am

Post 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!
Post Reply