HOW TO: How can I open an XLL from another XLL?

Reference: Q0029

Article last modified on 11-Jun-2006


The information in this article applies to:

  • XLL+ for Visual Studio 2005 - 5.0
  • XLL+ for Visual Studio .NET - 4.2, 4.3.1, 5.0
  • XLL+ for Visual Studio 6 - 3, 4.1, 4.2, 4.3.1, 5.0

HOW TO: How can I open an XLL from another XLL?

Question

My XLL needs to be sure that another XLL is open. How can I make sure that an XLL is opened?

Answer

Use the xlfRegister macro function to open an XLL and register all its methods. This macro will open the XLL properly, just as if it was opened through File/Open or Tools/Add-ins. The correct place to call the macro is probably during the handling of OnXllOpenEx() in your own XLL.

Note that this technique can only be used within a macro function or an event handler, not from within a worksheet function. It is therefore not possible to apply a "load-on-demand" strategy.

BOOL CBootstrapApp::OnXllOpenEx()
{
    // Calculate the full path of the bootstrapped XLL, which 
    // is assumed to be in the same directory as this XLL.
    CString strXll2 = GetXllName(); 
    int iSplit = strXll2.ReverseFind('\\');
    strXll2 = strXll2.Left(iSplit + 1) + "SimpOpt.xll";

    // Open the bootstrapped XLL
    static int xlfRegister = 149;
    CXlOper xloResult;
    int rc = xloResult.Excel(xlfRegister, 1, &CXlOper(strXll2));
    if (rc != 0)
    {
        XlMessageBox("Failed to open " + strXll2, XlMessageBoxTypeExclamation);
        return FALSE;
    }
    
    // Other start-up code...
    
    return TRUE;
}

See also

FAQ #0030 has information on calling add-in functions in another XLL.