關於BCB 連上 Excel OLE Server的問題! |
缺席
|
shchen
一般會員 發表:12 回覆:27 積分:7 註冊:2003-08-13 發送簡訊給我 |
請問各位前輩,關於BCB 使用OLE Server,連Excel的問題.
以下是我的code,可正常開啟Excel並載入檔案,但有一個小問題, 假使在使用OLE Server前,在Windows下以經有開啟別的xls檔案, 在執行OLE Server後,將會把原有的Excel視窗handle住變成OLE Server, 並且在關畢WorkBook時,會將所有的WorkBook都關畢了. 請問要怎樣才能逹到只關畢OLE Server所開啟的檔案,而不會也將原本已打開的WorkBooks關畢? void __fastcall TForm1::Button1Click(TObject *Sender) { int i,nbSheet; bool okWb; char buf[255]; Excel_2k::WorkbooksPtr books; Excel_2k::SheetsPtr sheets; Excel_2k::RangePtr cells; _WorkbookPtr wb; //ExcelWorkbookPtr wb; _WorksheetPtr ws; //ExcelWorksheetPtr ws; TVariant wbName,wsName,ExcelName; TVariantInParam readOnly, //bool numSheet,intValue, //int strValue; //string if(OpenDialog1->Execute()) { //opening ExcelApplication try { ExcelApplication1->Connect(); books=ExcelApplication1->Get_Workbooks(); ExcelApplication1->Set_Visible(TDefLCID(),true); } catch (Exception &exception) { Application->MessageBox( "This application needs Excel 2k or more", "Sorry...", MB_ICONSTOP); abort; } //Opening a Workbook in Excel wbName=OpenDialog1->FileName; try { //Open the existing Excel Workbook //following line to open with defaults values wb=books->Open(wbName); ExcelName="AutoConversion OLE Server"; ExcelApplication1->Caption=ExcelName; //Recover the collection of Worksheets sheets=wb->get_Sheets(); //and give each Worksheet a name nbSheet=sheets->Count; Label1->Caption=OpenDialog1->FileName; Label2->Caption="Total " IntToStr(sheets->Count) " sheet"; HWND hForm1=Form1->Handle; SetForegroundWindow(hForm1); ShowMessage("Now close Excel OLE Server!"); books->Close(); free(books);free(sheets);free(cells); ExcelApplication1->Disconnect(); } catch (Exception &exception) { Application->MessageBox( "Sure, there remains a problem anywhere !", "Sorry...", MB_ICONSTOP); abort; } } } 發表人 - shchen 於 2003/08/23 01:59:37 7年後回頭看看自己不懂的問題,已有解答: 只要將ExcelApplication1的ConnectKind設為ckNewInstance即可, 另提只要將Set_Visible(TDefLCID(),true); 設為Set_Visible(TDefLCID(),false);那OLE 出的EXCEL就能隱藏在背景執行不被user發現. 編輯記錄
shchen 重新編輯於 2010-06-02 23:04:43, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |