Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A month ago, I asked about the automation of MS word.
(Automation of already-running MS word[^])
and thanks to the members of codeproject, I solved my problem.

But at this time, it is Excel.
I tried the same thing with Excel and got an error message 'rejected by callee' (code
= 0x80010001).
When I trying to handle existing excel application, COM returns that error.
So I did googling and have read almost all articles about 'rejected by callee' and tried to fix that error.. it fails..

Please help me again with this..
Below is my simple code.

HWND hwnd = ::GetForegroundWindow();
if (hwnd == NULL)
    return;

USES_CONVERSION;

CLSID clsid;
HRESULT hr = CLSIDFromProgID(A2OLE(_T("Excel.Application")), &clsid);
if (hr != S_OK) {
    return;
}

IUnknown* pUnk;
hr = GetActiveObject(clsid, NULL, &pUnk);
if (hr == S_OK) {
    try {
        LPDISPATCH pDispatch = NULL;
        hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDispatch);
        ASSERT(!FAILED(hr));

        CApplication _iAppInterface;
        _iAppInterface.AttachDispatch(pDispatch);
        CRange _iRange = _iAppInterface.get_ActiveCell(); // <--- ERROR POINT
    }
    catch (COleDispatchException* pe) {
        pe->ReportError();
        pe->Delete();
    }
    catch (COleException* pe) {
        pe->ReportError();
        pe->Delete();
    }
    pUnk->Release();
}


What I have tried:

At the time above code runs in background, Excel is activated and I type a letter.
And my office365 Excel is fully licensed by the company I work for.
Thanks.
Posted
Comments
Maciej Los 21-Mar-22 7:58am    
I'd strongly recomend to read this: Application.ActiveCell property (Excel) | Microsoft Docs[^]
[EDIT]
At the time above code runs in background, Excel is activated and I type a letter.
While cell is in edition mode, you can not get active cell. You have to finish cell's edition to be able to return ActiveCell.
CHill60 21-Mar-22 8:40am    
Virtual 5 - likely to be the root cause
Maciej Los 21-Mar-22 9:22am    
Thank you, Caroline.
Member 14877890 22-Mar-22 5:06am    
Oops~ It is not a ActiveCell problem only, but every function raises an error also.. ActiveWindow, ActiveSheet.. and so on..
Then, Does it means that there is no action I can do with Excel Application while it is in Edit mode? T.T It's very SAD situation.
Hmm.. Anything I can do?
Thanks.
Maciej Los 22-Mar-22 7:38am    
If every method/property raises error, then Excel application initialization has failed. Check it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900