windowsexcelmfcactivexole

Need ActiveX control to embed Excel into a dialog


I'm building an "import from Excel" function. It has to be in a DLL, called from a non-MFC app. Has to provide an image of the spreadsheet that users can drag a select box around (to select cells), then click an IMPORT button, and have the right thing happen. Having trouble getting the spreadsheet up, having a button adjacent to it, and having it happen in a DLL.

I went the route of an MFC dialog inside a DLL, but was blocked in trying to bring up an OLE client for Excel on a window on the dialog. Only support I found for showing Excel using OLE required an SDI. I was able to get the automation functions to work, and I could read the cells and paint a "fake image" of them on my dialog... but I'm afraid that's going to be in non-compliance with my requirements.

So I tried creating an SDI. I was able to create an SDI that brought up Excel as an OLE Client. I was able to handle the "selection change" event, and go the cell data I needed. I was foiled at this point by 2 items:

  1. Could not make this SDI work in an MFC DLL. Crash in CWinApp constructor, asserting that the AfxGetThread wasn't null. Moved "theApp" to be locally scoped within the single exported DLL function, but still couldn't get it to work, but with different symptoms: crashed when added AFX_MANAGE_STATE(AfxGetStaticModuleState( )); to exported DLL function, if I didn't use that, the OnInitInstance never was entered. Tried adding a call to the app's Run() function, but that didn't help. I can post this code if someone thinks they have an idea what the problem is.

  2. Could not present a toolbar while the Excel client item was active. I could put my "import" (and other options) on the menubar at the top, but that's not evident enough. Even a floating toolbar was closed. It makes sense that it would do this, since it would be confusing to have a toolbar present that couldn't take actions on the currently active item... but in my case, it's a problem.

So now I'm thinking I need to go back to the dialog approach, and embed an ActiveX control that brings up an Excel spreadsheet file to import.

Must either allow me to handle the selection change event, or must allow me to find what cells are selected (or were most recently selected if clicking the "import" button on my dialog causes the selected cells to become unselected).

It must also come with reasonable licensing terms, as this will be used in a commercial application that is widely-available to the public.

I am also interested in "quick fixes" to either of the approaches I had been trying.


Solution

  • If you just want to embed Excel, you don't need anything.

    Just embed it into a standard OCX host control.