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

2

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;
}
c++
excel
automation
cell
asked on Stack Overflow Mar 27, 2013 by user2216761

1 Answer

0

You have to wrap the string value into a variant.

Try this:

pSheet->Range["C2"]->Value = _variant_t(_bstr_t("Hello")).Detach();
answered on Stack Overflow Jun 27, 2014 by EMike • edited Jun 27, 2014 by Ms. Nobody

User contributions licensed under CC BY-SA 3.0