set value to Excel cell in C++ throw exception 0x800A03EC

Mar 27, 2013 at 7:19 PM
I'm writing an Excel Addin. One of a UDF in the addin is to set value to a selected cell by using Excel Automation in C++. In my code, I have no problem to get range, read value from selected cell, but when I try to set value to cell, if the value is a string, the code throw exception (0x80020005 Type mismatch), otherwise, it always throw exception with HResult 0x800A03EC. Below is a code snippet:

Any ideas?
void SetValue()
{
   Excel::SheetsPtr pSheets = GetExcelApplicationObj()->GetWorksheets();
   Excel::_WorksheetPtr pSheet = GetExcelApplicationObj()->GetActiveSheet();

   _variant_t text = pSheet->Range["A2"]->Text; //Read value from selected cell works fine

   pSheet->Range["C2"]->Value = "Hello"; // throw 0x80020005 Type mismatch

   pSheet->Range["B2"]->Value = 5.0; //Set value throw Exception with HRESULT 0x800A03EC
}

Excel::_Application* GetExcelApplicationObj()
{
    if (m_pApp == NULL)
    {
        std::vector<HWND>::iterator it;
        std::vector<HWND> handles = getToplevelWindows();
        for (it = handles.begin(); it != handles.end(); it++)
        {
            HWND hwndChild = NULL;
            ::EnumChildWindows( (*it), EnumChildProc, (LPARAM)&hwndChild);
            if (hwndChild != NULL)
            {
                Excel::Window* pWindow = NULL;
                HRESULT hr = ::AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&pWindow);
                if (SUCCEEDED(hr))
                {
                    if (pWindow != NULL)
                    {
                        m_pApp = pWindow->GetApplication();
                        pWindow->Release();
                    }
                    break;
                }
            }
        }
    }
    return m_pApp;
}

std::vector<HWND> getToplevelWindows()
{
    EnumWindowsCallbackArgs args( ::GetCurrentProcessId() );
    if ( ::EnumWindows( &EnumWindowsCallback, (LPARAM) &args ) == FALSE ) {
      return std::vector<HWND>();
    }
    return args.handles;
}

BOOL CALLBACK EnumWindowsCallback( HWND hnd, LPARAM lParam )
{
    EnumWindowsCallbackArgs *args = (EnumWindowsCallbackArgs *)lParam;

    DWORD windowPID;
    (void)::GetWindowThreadProcessId( hnd, &windowPID );
    if ( windowPID == args->pid ) {
        args->handles.push_back( hnd );
    }

    return TRUE;
}

BOOL CALLBACK EnumChildProc(HWND hwndChild,LPARAM lParam)
{
    CHAR className[128];
    ::GetClassName(hwndChild, className, 128);
    if (strcmp(className, "EXCEL7") == 0)
    {
        HWND * phandle = (HWND*)lParam;
        (*phandle) = hwndChild;
        return FALSE;
    }
    return TRUE;
}
Coordinator
Mar 27, 2013 at 8:58 PM
Edited Mar 27, 2013 at 9:00 PM
Hi,

This CodePlex site is for Excel-DNA, which is a project to make Excel add-ins with .NET languages (like C#, VB.NET or F#). The best place to ask general Excel development questions is probably the Excel for Developers forum here: http://social.msdn.microsoft.com/Forums/en-US/exceldev/threads

If you want to make Excel add-ins in C++, I recommend you have a look at Keith Lewis's library here: http://xll.codeplex.com or search for the commercial XLL+ toolkit.

Anyway, I'm not sure how C++ does the COM interop, but perhaps you need to set the value using VARIANTs or BSTR value for strings.

-Govert