Click here to Skip to main content
15,921,774 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
JokeRe: Detect USB Pendrive Pin
Moak24-Sep-09 5:01
Moak24-Sep-09 5:01 
QuestionThread pool query Pin
Rajesh R Subramanian21-Sep-09 3:14
professionalRajesh R Subramanian21-Sep-09 3:14 
AnswerRe: Thread pool query Pin
Naveen21-Sep-09 4:04
Naveen21-Sep-09 4:04 
GeneralRe: Thread pool query Pin
Rajesh R Subramanian21-Sep-09 4:42
professionalRajesh R Subramanian21-Sep-09 4:42 
GeneralRe: Thread pool query Pin
Naveen21-Sep-09 7:10
Naveen21-Sep-09 7:10 
GeneralRe: Thread pool query Pin
Rajesh R Subramanian21-Sep-09 7:48
professionalRajesh R Subramanian21-Sep-09 7:48 
QuestionCell change event in Excel Pin
NarVish21-Sep-09 0:35
NarVish21-Sep-09 0:35 
AnswerRe: Cell change event in Excel Pin
Stuart Dootson21-Sep-09 2:35
professionalStuart Dootson21-Sep-09 2:35 
Here's a complete example that fires up Excel and handles some application events - the general principle (registering an object with Excel to handle its events) will be exactly the same for document events, except you'll register the handler with the relevant workbook, I guess and have to change the event handler object to suit the DocEvents as opposed to AppEvents. As you're new to C++, I'd advise you get a little more familiar with C++ (and the Microsoft specific bits of VC++) before diving into this too deeply.

#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 fucntion 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 *)
   {
      done_ = true;
   }
   void _stdcall AfterCalculate()
   {
      done_ = true;
   }
private:
   bool& done_;
};

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
         xl->Workbooks->Add();
         bool done = false;

         // Instantiate the 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);
         }
      }
   }
   // Tell the system we're done with COM
   CoUninitialize();
   return 0;
}


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

GeneralRe: Cell change event in Excel Pin
NarVish21-Sep-09 20:31
NarVish21-Sep-09 20:31 
GeneralRe: Cell change event in Excel Pin
NarVish23-Sep-09 23:26
NarVish23-Sep-09 23:26 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson24-Sep-09 5:57
professionalStuart Dootson24-Sep-09 5:57 
GeneralRe: Cell change event in Excel Pin
NarVish24-Sep-09 19:32
NarVish24-Sep-09 19:32 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson24-Sep-09 20:11
professionalStuart Dootson24-Sep-09 20:11 
GeneralRe: Cell change event in Excel Pin
NarVish24-Sep-09 21:26
NarVish24-Sep-09 21:26 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson25-Sep-09 4:03
professionalStuart Dootson25-Sep-09 4:03 
GeneralRe: Cell change event in Excel Pin
NarVish29-Sep-09 19:45
NarVish29-Sep-09 19:45 
GeneralRe: Cell change event in Excel Pin
NarVish30-Sep-09 18:29
NarVish30-Sep-09 18:29 
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 

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.