Click here to Skip to main content
12,699,531 members (32,900 online)

C / C++ / MFC

 
JokeRe: Detect USB Pendrive Pin
Moak24-Sep-09 6:01
memberMoak24-Sep-09 6:01 
QuestionThread pool query Pin
Rajesh R Subramanian21-Sep-09 4:14
mvpRajesh R Subramanian21-Sep-09 4:14 
AnswerRe: Thread pool query Pin
Naveen21-Sep-09 5:04
mvpNaveen21-Sep-09 5:04 
GeneralRe: Thread pool query Pin
Rajesh R Subramanian21-Sep-09 5:42
mvpRajesh R Subramanian21-Sep-09 5:42 
GeneralRe: Thread pool query Pin
Naveen21-Sep-09 8:10
mvpNaveen21-Sep-09 8:10 
GeneralRe: Thread pool query Pin
Rajesh R Subramanian21-Sep-09 8:48
mvpRajesh R Subramanian21-Sep-09 8:48 
QuestionCell change event in Excel Pin
NarVish21-Sep-09 1:35
memberNarVish21-Sep-09 1:35 
AnswerRe: Cell change event in Excel Pin
Stuart Dootson21-Sep-09 3:35
memberStuart Dootson21-Sep-09 3: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 21:31
memberNarVish21-Sep-09 21:31 
GeneralRe: Cell change event in Excel Pin
NarVish24-Sep-09 0:26
memberNarVish24-Sep-09 0:26 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson24-Sep-09 6:57
memberStuart Dootson24-Sep-09 6:57 
GeneralRe: Cell change event in Excel Pin
NarVish24-Sep-09 20:32
memberNarVish24-Sep-09 20:32 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson24-Sep-09 21:11
memberStuart Dootson24-Sep-09 21:11 
GeneralRe: Cell change event in Excel Pin
NarVish24-Sep-09 22:26
memberNarVish24-Sep-09 22:26 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson25-Sep-09 5:03
memberStuart Dootson25-Sep-09 5:03 
GeneralRe: Cell change event in Excel Pin
NarVish29-Sep-09 20:45
memberNarVish29-Sep-09 20:45 
GeneralRe: Cell change event in Excel Pin
NarVish30-Sep-09 19:29
memberNarVish30-Sep-09 19:29 
GeneralRe: Cell change event in Excel Pin
North 200915-Oct-09 2:38
memberNorth 200915-Oct-09 2:38 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson15-Oct-09 3:26
memberStuart Dootson15-Oct-09 3:26 
GeneralRe: Cell change event in Excel Pin
North 200915-Oct-09 3:43
memberNorth 200915-Oct-09 3:43 
GeneralRe: Cell change event in Excel Pin
North 200915-Oct-09 20:29
memberNorth 200915-Oct-09 20:29 
GeneralRe: Cell change event in Excel Pin
NarVish18-Oct-09 23:13
memberNarVish18-Oct-09 23:13 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson19-Oct-09 1:25
memberStuart Dootson19-Oct-09 1:25 
GeneralRe: Cell change event in Excel Pin
NarVish19-Oct-09 1:54
memberNarVish19-Oct-09 1:54 
GeneralRe: Cell change event in Excel Pin
Stuart Dootson19-Oct-09 2:09
memberStuart Dootson19-Oct-09 2: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.


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 20 Jan 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid