Click here to Skip to main content
Licence 
First Posted 20 Jul 2005
Views 45,846
Bookmarked 25 times

Get Cell Text of Excel

By | 25 Jul 2005 | Article
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

About the Author

windyloft



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExcel automation PinmemberRage21:38 25 Jul '05  
GeneralRe: Excel automation Pinmemberwindyloft22:18 25 Jul '05  
GeneralRe: Excel automation PinmemberRage1:43 26 Jul '05  
GeneralRe: Excel automation Pinmemberwindyloft14:46 26 Jul '05  
GeneralRe: Excel automation Pinmemberwindyloft14:50 26 Jul '05  
GeneralRe: Excel automation PinmemberMarsV15:38 26 Jul '05  
GeneralRe: Excel automation Pinmemberjanitor19780:12 1 Aug '05  
GeneralRe: Excel automation Pinmemberwindyloft14:11 1 Aug '05  
GeneralRe: Excel automation PinmemberMarsV14:46 1 Aug '05  
GeneralI'm modify the error about WREY. Pinmemberwindyloft21:34 25 Jul '05  
GeneralDoes not work. PinmemberWREY19:59 25 Jul '05  
GeneralYou're not disengaging your hook properly. PinmemberWREY6:05 26 Jul '05  
GeneralRe: You're not disengaging your hook properly. Pinmemberwindyloft14:37 26 Jul '05  
GeneralRe: Does not work. Pinmemberjanitor19780:08 1 Aug '05  
GeneralRe: Does not work. Pinmembertcy_tcy4:53 23 Feb '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 26 Jul 2005
Article Copyright 2005 by windyloft
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid