Click here to Skip to main content
15,887,214 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Cell change event in Excel Pin
NarVish15-Oct-09 1:38
NarVish15-Oct-09 1:38 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson15-Oct-09 2:26
professionalStuart Dootson15-Oct-09 2:26 
GeneralRe: Cell change event in Excel Pin
NarVish15-Oct-09 2:43
NarVish15-Oct-09 2:43 
GeneralRe: Cell change event in Excel Pin
NarVish15-Oct-09 19:29
NarVish15-Oct-09 19:29 
GeneralRe: Cell change event in Excel Pin
NarVish18-Oct-09 22:13
NarVish18-Oct-09 22:13 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson19-Oct-09 0:25
professionalStuart Dootson19-Oct-09 0:25 
GeneralRe: Cell change event in Excel Pin
NarVish19-Oct-09 0:54
NarVish19-Oct-09 0:54 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson19-Oct-09 1:09
professionalStuart Dootson19-Oct-09 1:09 
Firstly - be aware that Excel does dataflow analysis and will not call SheetCalculate if a change results in no recalculation (but as far as I can tell it ALWAYS calls AfterCalculate).

This (complete) program code successfully registers event handlers which get called by Excel:

#include <iostream>
#include <atlbase.h>
#include <atlcom.h>
// Reference the Excel object model
#import "libid:00020813-0000-0000-C000-000000000046" auto_search no_dual_interfaces rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") rename("DocumentProperties", "excelDocumentProperties") rename("SearchPath", "excelSearchPath") rename("CopyFile", "excelCopyFile") rename("ReplaceText", "excelReplaceText")

// Define the AppEvents handler object.

// First define function information for the events we want to handle
_ATL_FUNC_INFO SheetChangeInfo = { CC_CDECL, VT_EMPTY, 2, { VT_DISPATCH, VT_DISPATCH } };
_ATL_FUNC_INFO AfterCalculateInfo = { CC_CDECL, VT_EMPTY, 0, { } };

// And define an event handler object - note that this object just sets a boolean
// variable to true when a handled event is seen
class ExcelAppEventHandler : public IDispEventSimpleImpl<1, ExcelAppEventHandler, &__uuidof(Excel::AppEvents)>
{
public:
   ExcelAppEventHandler(bool& doneFlag) : done_(doneFlag) { done_ = false; }
   // The sink map maps event handler functions to the events they handle
   BEGIN_SINK_MAP(ExcelAppEventHandler)
      SINK_ENTRY_INFO(1, __uuidof(Excel::AppEvents), 0x0000061c, &ExcelAppEventHandler::SheetChange, &SheetChangeInfo)
      SINK_ENTRY_INFO(1, __uuidof(Excel::AppEvents), 0x00000a34, &ExcelAppEventHandler::AfterCalculate, &AfterCalculateInfo)
   END_SINK_MAP()

   // Event handling methods. These match the signatures given in the AppEvents
   // definition except for the return type and we've added in the _stdcall
   // calling convention.
   void _stdcall SheetChange(IDispatch *, struct Excel::Range *)
   {
      std::cout << "ExcelAppEventHandler::SheetChange\n";
   }
   void _stdcall AfterCalculate()
   {
      std::cout << "ExcelAppEventHandler::AfterCalculate\n";
   }
private:
   bool& done_;
};

_ATL_FUNC_INFO SheetCalculateInfo = { CC_CDECL, VT_EMPTY, 1, { VT_DISPATCH } };

class ExcelBookEventHandler : public IDispEventSimpleImpl<1, ExcelBookEventHandler, &__uuidof(Excel::WorkbookEvents)>
{
public:
   BEGIN_SINK_MAP(ExcelBookEventHandler)
      SINK_ENTRY_INFO(1, __uuidof(Excel::WorkbookEvents), 0x0000061b, &ExcelBookEventHandler::SheetCalculate, &SheetCalculateInfo)
   END_SINK_MAP()

   void _stdcall SheetCalculate(IDispatch *)
   {
      std::cout << "ExcelBookEventHandler::SheetCalculate\n";
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   // Tell the system we're using COM
   CoInitializeEx(0, COINIT_MULTITHREADED);
   {
      //Declare an Excel object pointer
      Excel::_ApplicationPtr xl;
      // And get a reference to the currently running instance of Excel
      if (SUCCEEDED(xl.GetActiveObject(__uuidof(Excel::Application))))
      {
         // Make sure there's at least one workbook
         Excel::_WorkbookPtr wb = xl->Workbooks->Add();
         ExcelBookEventHandler bookHandler;
         bookHandler.DispEventAdvise(wb);
         Excel::_WorksheetPtr ws = wb->Worksheets->Item[1];
         if (ws)
            std::cout << ws->Name << std::endl;
         bool done = false;

         // Instantiate teh event handler
         ExcelAppEventHandler handler(done);
         // Tell Excel about the handler
         if (SUCCEEDED(handler.DispEventAdvise(xl)))
         {
            // Run a Windows message loop to pump COM messages - this is needed
            // because that's how COM works...
            MSG msg;
            while (!done && GetMessage(&msg, NULL, 0, 0) > 0) {
               TranslateMessage(&msg);
               DispatchMessage(&msg);
            }            
            // Tell Excel we don't want to handle events any more.
            handler.DispEventUnadvise(xl);
         }
      }
   }
   CoUninitialize();
   return 0;
}


Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

Questioninterfacing google earth with c++/MFC app Pin
brian scott21-Sep-09 0:20
brian scott21-Sep-09 0:20 
QuestionWriting unicode to file Pin
yeah100021-Sep-09 0:12
yeah100021-Sep-09 0:12 
AnswerRe: Writing unicode to file Pin
Nuri Ismail21-Sep-09 0:19
Nuri Ismail21-Sep-09 0:19 
GeneralRe: Writing unicode to file Pin
yeah100021-Sep-09 0:42
yeah100021-Sep-09 0:42 
AnswerRe: Writing unicode to file Pin
Naveen21-Sep-09 1:12
Naveen21-Sep-09 1:12 
GeneralRe: Writing unicode to file Pin
yeah100021-Sep-09 1:56
yeah100021-Sep-09 1:56 
GeneralRe: Writing unicode to file Pin
Naveen21-Sep-09 2:27
Naveen21-Sep-09 2:27 
AnswerRe: Writing unicode to file Pin
David Crow21-Sep-09 2:15
David Crow21-Sep-09 2:15 
GeneralRe: Writing unicode to file Pin
yeah100021-Sep-09 2:30
yeah100021-Sep-09 2:30 
GeneralRe: Writing unicode to file Pin
David Crow21-Sep-09 2:35
David Crow21-Sep-09 2:35 
GeneralRe: Writing unicode to file Pin
yeah100021-Sep-09 2:43
yeah100021-Sep-09 2:43 
GeneralRe: Writing unicode to file Pin
David Crow21-Sep-09 3:20
David Crow21-Sep-09 3:20 
QuestionCreate shortcut Pin
gdctld20-Sep-09 23:46
gdctld20-Sep-09 23:46 
AnswerRe: Create shortcut Pin
CPallini21-Sep-09 0:11
mveCPallini21-Sep-09 0:11 
AnswerRe: Create shortcut Pin
Nuri Ismail21-Sep-09 0:11
Nuri Ismail21-Sep-09 0:11 
Questionwindows service Pin
jinjiashan20-Sep-09 23:40
jinjiashan20-Sep-09 23:40 
AnswerRe: windows service Pin
Naveen21-Sep-09 0:10
Naveen21-Sep-09 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.