在我的程序中,新建了一个EXCEL,想通过连接点获得EXCEL的消息,但是就是没有反映,不知道为什么,下面是我的代码:
if(!m_app.CreateDispatch("Excel.Application.9")) {
AfxMessageBox("Excel 2000 program not found");
return ;
   }   
LPDISPATCH pWorkbooks;    
m_app.SetSheetsInNewWorkbook(1);
 
CWorkbooks workbooks;
VERIFY(pWorkbooks = m_app.GetWorkbooks ());
workbooks.AttachDispatch(pWorkbooks);

LPDISPATCH pWorkbook = NULL;
if (workbooks.get_Count() == 0) {
pWorkbook = workbooks.Add(); // Save the pointer for
//  later release
} LPDISPATCH pWorksheets = m_app.GetWorksheets();
ASSERT(pWorksheets != NULL);
CWorksheets worksheets;
worksheets.AttachDispatch(pWorksheets); LPDISPATCH pWorksheet = m_app.GetActiveSheet();//(COleVariant((short)1)); m_sheet.AttachDispatch(pWorksheet);// Make server visible through automation.
      // I.e.: Application.Visible = TRUE
      DISPID dispID;
      unsigned short *ucPtr;
      BYTE *parmStr;
      ucPtr = L"visible";
      m_app.m_lpDispatch->GetIDsOfNames(
           IID_NULL, &ucPtr, 1, LOCALE_USER_DEFAULT, &dispID
      );
      parmStr = (BYTE *)( VTS_VARIANT );
      m_app.InvokeHelper(
         dispID, DISPATCH_METHOD | DISPATCH_PROPERTYPUT, VT_EMPTY,
         NULL, parmStr, &COleVariant((short)TRUE)
      ); HRESULT hr;
IConnectionPointContainer *pConnPtContainer; hr = m_sheet.m_lpDispatch->QueryInterface(IID_IConnectionPointContainer,(void **)&pConnPtContainer);
ASSERT(!FAILED(hr));

hr = pConnPtContainer->FindConnectionPoint(IID_IDocEvents,&m_pConnectionPoint);
ASSERT(!FAILED(hr));
         //m_myEventSink我类,他 的GUID和IID_IDocEvents是一样的
LPUNKNOWN pUnk = m_myEventSink.GetInterface(&IID_IUnknown);
ASSERT(pUnk); hr = m_pConnectionPoint->Advise(pUnk, &m_adviseCookie);
ASSERT(!FAILED(hr)); pConnPtContainer->Release();  if(pWorkbook!=NULL)
pWorkbook->Release();
我用的是EXCEL2000,调试没有错误,但是就是不能响应消息。
我参考的是http://support.microsoft.com/kb/183599,它使用的是Word我试了可以,但是为什么excel就不可以,问各位高手我哪有问题么?

解决方案 »

  1.   

    HOWTO: Catch Microsoft Excel Application Events Using VC++ Q186427
    Steps to Create Project
    Create a new dialog box-based application using the MFC AppWizard. Name your project ExcelEvents, and accept the default settings. 
    Add the following public member variables to your ExcelEventsDlg class in ExcelEventsDlg.h:      COleDispatchDriver m_app;
          IConnectionPoint *m_pConnectionPoint;
          DWORD m_adviseCookie; 
    Add two command buttons to your dialog box and name them "Start and Setup" and "Quit and Clean Up," respectively. 
    Add the following code to a handler for the Start and Setup button:      // Check to see if you've already started the server.
          if(m_app.m_lpDispatch != NULL) {
             AfxMessageBox("Server already started.");
             return;
          }      char buf[256]; // General purpose buffer.      // Start the Automation server.
          COleException e;
          if(!m_app.CreateDispatch("Excel.Application", &e)) {
             sprintf(buf, "Error on CreateDispatch(): %ld (%08lx)",
               e.m_sc, e.m_sc);
             AfxMessageBox(buf, MB_SETFOREGROUND);
             return;
          }      // Make the server visible through automation.
          // i.e.: Application.Visible = TRUE
          DISPID dispID;
          unsigned short *ucPtr;
          BYTE *parmStr;
          ucPtr = L"visible";
          m_app.m_lpDispatch->GetIDsOfNames(
               IID_NULL, &ucPtr, 1, LOCALE_USER_DEFAULT, &dispID
          );
          parmStr = (BYTE *)( VTS_VARIANT );
          m_app.InvokeHelper(
             dispID, DISPATCH_METHOD | DISPATCH_PROPERTYPUT, VT_EMPTY,
             NULL, parmStr, &COleVariant((short)TRUE)
          );      // Declare the events you want to catch.      // {00024413-0000-0000-C000-000000000046}
          static const GUID IID_IExcel8AppEvents =
          {0x00024413,0x000,0x0000,{0xc0,0x00,0x0,0x00,0x00,0x00,0x00,0x46 } };      // Steps for setting up events:
          // 1. Get server's IConnectionPointContainer interface.
          // 2. Call IConnectionPointContainer::FindConnectionPoint()
          //    to find the event you want to catch.
          // 3. Call IConnectionPoint::Advise() with the IUnknown
          //    interface of your implementation of the events.      HRESULT hr;      // Get server's IConnectionPointContainer interface.
          IConnectionPointContainer *pConnPtContainer;
          hr = m_app.m_lpDispatch->QueryInterface(
             IID_IConnectionPointContainer,
             (void **)&pConnPtContainer
          );
          ASSERT(!FAILED(hr));      // Find the connection point for events you're interested in.
          hr = pConnPtContainer->FindConnectionPoint(
             IID_IExcel8AppEvents,
             &m_pConnectionPoint
          );
          ASSERT(!FAILED(hr));      // Setup advisory connection.
          hr = m_pConnectionPoint->Advise(&g_XLEventDispatch, &m_adviseCookie);
          ASSERT(!FAILED(hr));      // Release IConnectionPointContainer interface.
          pConnPtContainer->Release(); 
      

  2.   

    楼上的方法和我贴的基本一样啊
    hr = pConnPtContainer->FindConnectionPoint(
             IID_IExcel8AppEvents,
             &m_pConnectionPoint
          );
          ASSERT(!FAILED(hr));
    会报错呢,hr返回的可不是0啊
      

  3.   

    HOWTO: Catch Microsoft Excel Application Events Using VC++ Q186427
    see this KB articles
      

  4.   

    在哪看啊,MSDN里面好象没有啊