我的問題是在標題中錯誤消息:C++ - 已從其基礎RCW分開COM對象不能使用
COM object that has been separated from its underlying RCW cannot be used.
我試圖谷歌,但我只能找到相關的解決方案析構函數/釋放對象。 這不是我想做的事情。
首先,這裏是我的代碼:
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]);
}
}
}
簡而言之:我有一個checkedlistbox,當必要的東西已經被選定的這部分代碼運行。在checkedlistbox中列出了Excel工作簿的不同工作表。我試圖根據在checkedlistbox中選擇的項目從工作簿中選擇工作表。
在最後的代碼行(exWss = safe_cast(exApp-> ActiveWorkbook-> Sheets [p]);)我得到了提到的錯誤消息。
我已經看到元帥釋放會導致這樣的問題。在另一個函數,雖然我運行Marshal.Releasecomobject,但它是完全分開的,我也得到了同樣的錯誤。
任何想法,將不勝感激。