65.9K
CodeProject is changing. Read more.
Home

Get Cell Text of Excel

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.90/5 (9 votes)

Jul 21, 2005

2 min read

viewsIcon

72670

downloadIcon

2128

This article introduce how use hook and Com to get cell text of Excel2000.

Sample Image - hook.jpg

Introduction

Open a Excel Book, and when use mouse click cell of Excel, get the context of cell.

Step 1:

       Build a Dll for Globe Hook. Because I need use a Globe Hook, Build Dll to Set the Hook.

DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
 // Remove this if you use lpReserved
 UNREFERENCED_PARAMETER(lpReserved);

 if (dwReason == DLL_PROCESS_ATTACH)
 {
  TRACE0("HOOK.DLL Initializing!\n");
  
  // Extension DLL one-time initialization
  if (!AfxInitExtensionModule(HookDLL, hInstance))
   return 0;

  // Insert this DLL into the resource chain
  // NOTE: If this Extension DLL is being implicitly linked to by
  //  an MFC Regular DLL (such as an ActiveX Control)
  //  instead of an MFC application, then you will want to
  //  remove this line from DllMain and put it in a separate
  //  function exported from this Extension DLL.  The Regular DLL
  //  that uses this Extension DLL should then explicitly call that
  //  function to initialize this Extension DLL.  Otherwise,
  //  the CDynLinkLibrary object will not be attached to the
  //  Regular DLL's resource chain, and serious problems will
  //  result.

  new CDynLinkLibrary(HookDLL);
  gInstance = hInstance;
 }
 else if (dwReason == DLL_PROCESS_DETACH)
 {
  TRACE0("HOOK.DLL Terminating!\n");
  // Terminate the library before destructors are called
  AfxTermExtensionModule(HookDLL);
 }
 return 1;   // ok
}

Step 2:

Select a Hook, In this case I use WH_GETMESSAGE HOOK.

Use Hook to get currently Hanle of Application, and judge the Handle if or not is you need.

if the handle is you need, use Com to get Cell Text of Excel2000.

gHGHook = SetWindowsHookEx(WH_GETMESSAGE, lpfn, gInstance, 0);

Fllow is HOOK function:

LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
{
 MSG *pMsg = (MSG *)lParam;
 char szClassName[128];
 
 memset(szClassName, 0, sizeof(szClassName));
 
 if (pMsg->message == WM_LBUTTONUP)
 {
  if (JudgeRlation((HWND)gExcel, pMsg->hwnd) == 0)
  {
   ::GetClassName(pMsg->hwnd, szClassName, sizeof(szClassName)-1);

   if(strcmp(szClassName, "EXCEL7") == 0)
   {
    COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
 
    _Application objApp;
    Window objWin;

    Range objRange;
    VARIANT ret;
    
    CLSID clsid ;
    if (CLSIDFromProgID(OLESTR("Excel.Application.9"), &clsid) == NOERROR)
    {
     // Recherche du server Excel s'il est lance
     LPUNKNOWN lpUnk ;
     LPDISPATCH lpDispatch ;
     if (GetActiveObject(clsid, NULL, &lpUnk) == S_OK)
     {
      HRESULT hr = lpUnk->QueryInterface(IID_IDispatch, (LPVOID*)&lpDispatch) ;
      lpUnk->Release() ;
      if (hr == NOERROR)
      {
       objApp.AttachDispatch(lpDispatch, TRUE) ;
      }
     }
    }

    objWin = objApp.GetActiveWindow();
    objRange = objWin.GetActiveCell();
    ret = objRange.GetValue();

    BSTR txt;
    txt = ret.bstrVal;
    char *p=_com_util::ConvertBSTRToString(txt);

    ::SendMessage(gApp, WM_SETTEXT, 0, (LPARAM)p);
   }
  }
 }
 else if (pMsg->message == WM_LBUTTONDOWN)
 {
 }
 
 return CallNextHook(WH_GETMESSAGE, code, wParam, lParam);
}

Step 3:

Uninstall HOOK.

int UnHook(int idHook)
{
 if (idHook == WH_GETMESSAGE)
 {
  if(gHGHook == NULL)
  {
   return -1;
  }
  UnhookWindowsHookEx(gHGHook);
  gHGHook = NULL;
 }
 
 return 0;
}

Step 4:

Usage Com get the Cell Text of Excel Book .

COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
 
    _Application objApp;
    Window objWin;

    Range objRange;
    VARIANT ret;
    
    CLSID clsid ;
    if (CLSIDFromProgID(OLESTR("Excel.Application.9"), &clsid) == NOERROR)
    {
     // Recherche du server Excel s'il est lance
     LPUNKNOWN lpUnk ;
     LPDISPATCH lpDispatch ;
     if (GetActiveObject(clsid, NULL, &lpUnk) == S_OK)
     {
      HRESULT hr = lpUnk->QueryInterface(IID_IDispatch, (LPVOID*)&lpDispatch) ;
      lpUnk->Release() ;
      if (hr == NOERROR)
      {
       objApp.AttachDispatch(lpDispatch, TRUE) ;
      }
     }
    }

    objWin = objApp.GetActiveWindow();
    objRange = objWin.GetActiveCell();
    ret = objRange.GetValue();

    BSTR txt;
    txt = ret.bstrVal;
    char *p=_com_util::ConvertBSTRToString(txt);

    ::SendMessage(gApp, WM_SETTEXT, 0, (LPARAM)p);