Click here to Skip to main content
15,894,896 members
Articles / Desktop Programming / MFC
Article

Get Cell Text of Excel

Rate me:
Please Sign up or sign in to vote.
1.90/5 (9 votes)
25 Jul 20052 min read 72.3K   2.1K   28   15
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);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions

 
GeneralExcel automation Pin
Rage25-Jul-05 21:38
professionalRage25-Jul-05 21:38 
GeneralRe: Excel automation Pin
windyloft25-Jul-05 22:18
windyloft25-Jul-05 22:18 
GeneralRe: Excel automation Pin
Rage26-Jul-05 1:43
professionalRage26-Jul-05 1:43 
GeneralRe: Excel automation Pin
windyloft26-Jul-05 14:46
windyloft26-Jul-05 14:46 
GeneralRe: Excel automation Pin
windyloft26-Jul-05 14:50
windyloft26-Jul-05 14:50 
GeneralRe: Excel automation Pin
MarsV26-Jul-05 15:38
MarsV26-Jul-05 15:38 
GeneralRe: Excel automation Pin
janitor19781-Aug-05 0:12
janitor19781-Aug-05 0:12 
GeneralRe: Excel automation Pin
windyloft1-Aug-05 14:11
windyloft1-Aug-05 14:11 
GeneralRe: Excel automation Pin
MarsV1-Aug-05 14:46
MarsV1-Aug-05 14:46 
GeneralI'm modify the error about WREY. Pin
windyloft25-Jul-05 21:34
windyloft25-Jul-05 21:34 
GeneralDoes not work. Pin
WREY25-Jul-05 19:59
WREY25-Jul-05 19:59 
GeneralYou're not disengaging your hook properly. Pin
WREY26-Jul-05 6:05
WREY26-Jul-05 6:05 
GeneralRe: You're not disengaging your hook properly. Pin
windyloft26-Jul-05 14:37
windyloft26-Jul-05 14:37 
GeneralRe: Does not work. Pin
janitor19781-Aug-05 0:08
janitor19781-Aug-05 0:08 
GeneralRe: Does not work. Pin
tcy_tcy23-Feb-09 4:53
tcy_tcy23-Feb-09 4:53 

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.