Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. IDispatch

IDispatch

Scheduled Pinned Locked Moved COM
16 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    trioum
    wrote on last edited by
    #1

    I want to get the already open excel sheet and get the cell value using IDispatch.

    Trioum

    S C 2 Replies Last reply
    0
    • T trioum

      I want to get the already open excel sheet and get the cell value using IDispatch.

      Trioum

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      Start by reading up on the Running Object Table[^].

      Steve

      1 Reply Last reply
      0
      • T trioum

        I want to get the already open excel sheet and get the cell value using IDispatch.

        Trioum

        C Offline
        C Offline
        Cool_Dev
        wrote on last edited by
        #3

        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");
        

        }

        T 1 Reply Last reply
        0
        • C Cool_Dev

          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");
          

          }

          T Offline
          T Offline
          trioum
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • T 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

            C Offline
            C Offline
            Cool_Dev
            wrote on last edited by
            #5

            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.

            T 1 Reply Last reply
            0
            • C Cool_Dev

              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.

              T Offline
              T Offline
              trioum
              wrote on last edited by
              #6

              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

              C 1 Reply Last reply
              0
              • T 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

                C Offline
                C Offline
                Cool_Dev
                wrote on last edited by
                #7

                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:

                T 1 Reply Last reply
                0
                • C Cool_Dev

                  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:

                  T Offline
                  T Offline
                  trioum
                  wrote on last edited by
                  #8

                  but i am not getting the get_text() method . in this function .

                  Trioum

                  C 1 Reply Last reply
                  0
                  • T trioum

                    but i am not getting the get_text() method . in this function .

                    Trioum

                    C Offline
                    C Offline
                    Cool_Dev
                    wrote on last edited by
                    #9

                    how did u try it? please show a bit of code..

                    T 1 Reply Last reply
                    0
                    • C Cool_Dev

                      how did u try it? please show a bit of code..

                      T Offline
                      T Offline
                      trioum
                      wrote on last edited by
                      #10

                      STDMETHODIMP CAppEventListener::HandleSheetChange( IDispatch* xlSheet, IDispatch* xlRange) { OutputDebugString("HandleSheetChange\n"); xlRange-> here not gettting text method HRESULT hr = S_OK; return hr; }

                      Trioum

                      C 1 Reply Last reply
                      0
                      • T trioum

                        STDMETHODIMP CAppEventListener::HandleSheetChange( IDispatch* xlSheet, IDispatch* xlRange) { OutputDebugString("HandleSheetChange\n"); xlRange-> here not gettting text method HRESULT hr = S_OK; return hr; }

                        Trioum

                        C Offline
                        C Offline
                        Cool_Dev
                        wrote on last edited by
                        #11

                        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. ---- :)

                        T 1 Reply Last reply
                        0
                        • C Cool_Dev

                          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. ---- :)

                          T Offline
                          T Offline
                          trioum
                          wrote on last edited by
                          #12

                          Done it . but exe is crashing as I call getText()

                          Trioum

                          T 1 Reply Last reply
                          0
                          • T trioum

                            Done it . but exe is crashing as I call getText()

                            Trioum

                            T Offline
                            T Offline
                            trioum
                            wrote on last edited by
                            #13

                            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

                            C 1 Reply Last reply
                            0
                            • T trioum

                              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

                              C Offline
                              C Offline
                              Cool_Dev
                              wrote on last edited by
                              #14

                              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.. :) }

                              T 1 Reply Last reply
                              0
                              • C Cool_Dev

                                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.. :) }

                                T Offline
                                T Offline
                                trioum
                                wrote on last edited by
                                #15

                                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

                                T 1 Reply Last reply
                                0
                                • T 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

                                  T Offline
                                  T Offline
                                  trioum
                                  wrote on last edited by
                                  #16

                                  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

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups