Let’s say we have created an ActiveX control using the wizard in Visual Studio 2008, and have checked the “Available in Insert Object Dialog” checkbox on the last page of the wizard as shown below:

As the name suggests, this setting enables the ActiveX control to be in “Insert Object” dialog box, which is available in most Compound Document container applications, such as, Wordpad, Word, Excel, PowerPoint, etc.
Now, when we insert such an ActiveX to Excel 2003 (using this Insert Object dialog box) while we are recording a macro; Excel may crash.
This happens because the default MFC implementation of the ActiveX enables it to capture the focus when instantiated. So, when we insert it to Excel while recording a macro, our ActiveX gets the focus and this causes Excel to crash.
If our ActiveX is not a compound document object (like Word document, PowerPoint spreadsheet etc.) then we can avoid this crash by modifying our ActiveX code generated by the MFC wizard. Here are the steps to achieve this:
- Open the ActiveXCtrl.cpp file (here ActiveX is the name that you have given to your control).
- Scroll down to the following comment and code block:
- Now OR a new flag OLEMISC_NOUIACTIVATE to the above list, as below:
// Control type information
static const DWORD BASED_CODE _dwAxTest1OleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
// Control type information
static const DWORD BASED_CODE _dwAxTest1OleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE |
OLEMISC_NOUIACTIVATE;
This flag ensures that our ActiveX will not get activated (or gain focus) when it is instantiated, thereby avoiding the crash in Excel 2003.