My problem is the error message in the title:
COM object that has been separated from its underlying RCW cannot be used.
I have tried to google it, but i could find only solutions related to destructors/releasing objects.
It is not something i want to do.
First, here is my code:
String^ filename="c:\\wb.xlsx";
Microsoft::Office::Interop::Excel::Application^ exApp= gcnew Microsoft::Office::Interop::Excel::ApplicationClass();
exApp->Visible=false;
exApp->Workbooks->Open(filename, 2, true, Type::Missing, Type::Missing, Type::Missing, true, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing);
Worksheet^ exWss;
for (int x = 0; x <= checkedListBox1->CheckedItems->Count - 1; x++){
for (int p=0; p<checkedListBox1->Items->Count; p++){
if (checkedListBox1->CheckedItems[x]->ToString()->Equals(checkedListBox1->Items[p]->ToString())){
p++;
exWss = safe_cast<Worksheet^> (exApp->ActiveWorkbook->Sheets[p]);
}
}
}
In short: i have a checkedlistbox, this part of code is ran when the necessary stuff have been selected. In the checkedlistbox the different sheets of the Excel workbook are listed. I am trying to select the sheet from the workbook according to the selected item in the checkedlistbox.
At the last code line (exWss = safe_cast (exApp->ActiveWorkbook->Sheets[p]);) I get the mentioned error message.
I have seen that Marshal releasing can cause such problems. In another function although i run Marshal.Releasecomobject, but it is completely separated, and i get the same error without it also.
Any ideas would be appreciated.