How do I call an add-in function in one XLL from another XLL

362 Views Asked by At

I have made an XLL+ user defined function, sum1, and it can run smoothly in Excel after I add this add-in. But now I am building another XLL, say sum2. I wish I can call sum1 in this add in sum2. As sum1 and sum2 are in different XLL, they can not be called directly and need some codes to do that. Does any one meet this problem before and any good way to do that?

I googled the problem and found the codes below, which is done by evaluate and UDF function. But it seems the code is very old and is suitable for visual studio 2005 but not for 2012 and I can get the #Name error when I run it.

Will be really appreciate if someone can help me on this. Thank you very much.

// Function:    CalDaysInYear2
// Purpose:     Calls a function in another XLL

//{{XLP_SRC(CalDaysInYear2)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(CalDaysInYear2, "RI", "CalDaysInYear2", 
    "DayCount", "Date & Time", "Calls a function in another XLL,"
    " which returns number of days in a year according to the"
    " day count", "Day count convention\000", "\0appscope=1\0",
    1)

extern "C" __declspec( dllexport )
LPXLOPER CalDaysInYear2(short DayCount)
{
    XLL_FIX_STATE;
    CXlOper xloResult;
//}}XLP_SRC

    static int xlfEvaluate = 257;
    static int xlUDF = 255;

    CXlOper xloName, xloRef;
    int rc = 0;
    xloName = "CalDaysInYear";`enter code here`
    if (!rc)
        rc = xloRef.Excel(xlfEvaluate, 1, &xloName);
    if (!rc)
        rc = xloResult.Excel(xlUDF, 2, &xloRef, &CXlOper(DayCount, 0));

    return xloResult.Ret();
}
1

There are 1 best solutions below

0
On

The first important point to note is that an XLL is a DLL.

When you build the XLL(DLL) Visual studio should also create a LIB file with the same basename (ie If the first is called project1.XLL then it should also create project1.LIB).

If you add a header file with the appropriate function definition to your second project, and also add the library created with first project to your second project then VS should take care of the dirty bits of calling GetProcAddress() etc.

extern "C" __declspec( dllimport ) 
LPXLOPER CalDaysInYear2(short DayCount);

Note, the header for the second project uses dllimport not dllexport.