IDispatch
-
Start by reading up on the Running Object Table[^].
Steve
-
CoInitialize(NULL);
HRESULT hr;
CLSID clsidExcelApp;// get CLSID of Excel Application
hr = CLSIDFromProgID(L"Excel.Application", &clsidExcelApp);IUnknown *pUnk = 0;
// get running object that has been registered with OLE.
hr = GetActiveObject(clsidExcelApp, NULL, &pUnk);
if(SUCCEEDED(hr))
{
IDispatch *pDisp = 0;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);//use class wizard generated excel type lib classes.. CApplication oApp(pDisp); CWorkbooks oBooks = oApp.get\_Workbooks(); CWorkbook oBook = oBooks.get\_Item(COleVariant((short)1)); //First workbook CWorksheets oSheets = oBook.get\_Worksheets(); CWorksheet oSheet = oSheets.get\_Item(COleVariant((short)1)); //First work sheet oSheet.Activate(); CRange oRange = oSheet.get\_Range(COleVariant(CString(L"A1")), COleVariant(CString(L"A1"))); //cell A1 COleVariant var = oRange.get\_Text(); MessageBox(var.bstrVal, L"Text in cell A1");
}
-
CoInitialize(NULL);
HRESULT hr;
CLSID clsidExcelApp;// get CLSID of Excel Application
hr = CLSIDFromProgID(L"Excel.Application", &clsidExcelApp);IUnknown *pUnk = 0;
// get running object that has been registered with OLE.
hr = GetActiveObject(clsidExcelApp, NULL, &pUnk);
if(SUCCEEDED(hr))
{
IDispatch *pDisp = 0;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);//use class wizard generated excel type lib classes.. CApplication oApp(pDisp); CWorkbooks oBooks = oApp.get\_Workbooks(); CWorkbook oBook = oBooks.get\_Item(COleVariant((short)1)); //First workbook CWorksheets oSheets = oBook.get\_Worksheets(); CWorksheet oSheet = oSheets.get\_Item(COleVariant((short)1)); //First work sheet oSheet.Activate(); CRange oRange = oSheet.get\_Range(COleVariant(CString(L"A1")), COleVariant(CString(L"A1"))); //cell A1 COleVariant var = oRange.get\_Text(); MessageBox(var.bstrVal, L"Text in cell A1");
}
I good I get it . But I am facing two problems (a) : I am not able to get the cell change event . i.e. as the value in cell change My application get the event that which is cell i.e. its row and col . (b) as I click on any cell in edit mode application hang .
Trioum
-
I good I get it . But I am facing two problems (a) : I am not able to get the cell change event . i.e. as the value in cell change My application get the event that which is cell i.e. its row and col . (b) as I click on any cell in edit mode application hang .
Trioum
Get IConnectionPointContainer interface of the application object, and get connection point to the events you want to catch. In this case, i think you have to deal with WorkBookEvents and DocEvents. Several documentations are available.. catching events from word handling events for excel create sink event for COM client Use OLE-COM object viewer to get exact UUID of the events you want to trap. best of luck.
-
Get IConnectionPointContainer interface of the application object, and get connection point to the events you want to catch. In this case, i think you have to deal with WorkBookEvents and DocEvents. Several documentations are available.. catching events from word handling events for excel create sink event for COM client Use OLE-COM object viewer to get exact UUID of the events you want to trap. best of luck.
I got the event of sheet change but still not getting the value of cell and its location i.e. row and col of cell in the function . STDMETHODIMP CAppEventListener::HandleSheetChange( IDispatch* xlSheet, IDispatch* xlRange) { OutputDebugString("HandleSheetChange\n"); HRESULT hr = S_OK; return hr; }
Trioum
-
I got the event of sheet change but still not getting the value of cell and its location i.e. row and col of cell in the function . STDMETHODIMP CAppEventListener::HandleSheetChange( IDispatch* xlSheet, IDispatch* xlRange) { OutputDebugString("HandleSheetChange\n"); HRESULT hr = S_OK; return hr; }
Trioum
-
did u examine the xlRange value? I think it will contain the cell range where the event is fired. Try calling get_Text() on this range to get text in the cell.. see various methods on Range object.. :thumbsup:
-
STDMETHODIMP CAppEventListener::HandleSheetChange( IDispatch* xlSheet, IDispatch* xlRange) { OutputDebugString("HandleSheetChange\n"); xlRange-> here not gettting text method HRESULT hr = S_OK; return hr; }
Trioum
oh.. You did exactly as i guessed :doh: . xlRange is Range object's IDispatch iterface. If you haven't already done, add a Range class (say CRange) to the project from EXCEL.exe using 'Add MFC Class from Typelib' wizard. Then CRange oRange(xlRange); See the methods on Range object now by using oRange. ---- :)
-
oh.. You did exactly as i guessed :doh: . xlRange is Range object's IDispatch iterface. If you haven't already done, add a Range class (say CRange) to the project from EXCEL.exe using 'Add MFC Class from Typelib' wizard. Then CRange oRange(xlRange); See the methods on Range object now by using oRange. ---- :)
-
Now by lot of R&D problem is solved . Can you guide me for SheetCalculate event . its example is not given on the site.
Trioum
well done :thumbsup: OLE automation may need some R&D most time. You can catch SheetCalculate event in much the same way you did for SheetChange event. Mean, all reside in IAppEvent interface, so Advice() for it, and override the SheetCaluclate method.
HRESULT _stdcall SheetCalculate(IDispatch* pSheetDisp) { //let CWorkSheet is the wrapper of _WorkSheet CWorkSheet oSheet(pSeetDisp); //do whatever you want to do here.. :) }
-
well done :thumbsup: OLE automation may need some R&D most time. You can catch SheetCalculate event in much the same way you did for SheetChange event. Mean, all reside in IAppEvent interface, so Advice() for it, and override the SheetCaluclate method.
HRESULT _stdcall SheetCalculate(IDispatch* pSheetDisp) { //let CWorkSheet is the wrapper of _WorkSheet CWorkSheet oSheet(pSeetDisp); //do whatever you want to do here.. :) }
I got it and do it , but facing one more problem as in case on "change event" I got the cell number i.e its row and column in which value is change . But "calculate event" fire when there is reference of another cell or cell containing formulae and when value change in such cell in this case I am not getting the cell no i.e cell row and column . this is my problem it become very tedious when I have the thousand of such cells having reference of various cells and formulaes. In this case I have to parse every cell to find out the value change . this become very slow for my project .I am searching the way to find out the cell row and column when there is calculate event fire .
Trioum
-
I got it and do it , but facing one more problem as in case on "change event" I got the cell number i.e its row and column in which value is change . But "calculate event" fire when there is reference of another cell or cell containing formulae and when value change in such cell in this case I am not getting the cell no i.e cell row and column . this is my problem it become very tedious when I have the thousand of such cells having reference of various cells and formulaes. In this case I have to parse every cell to find out the value change . this become very slow for my project .I am searching the way to find out the cell row and column when there is calculate event fire .
Trioum
Now I am facing one more problem . I have multiple excel file runnig on one system , but I am getting only one excel file using GetActiveObject. when I close this then I am getting second one using get object . so why I am not getting all the excel files ??
Trioum