XavierA
XavierA

Reputation: 1775

Packed Excel DNA .xll file does not load Ribbon

I try to deploy an Excel Add-In built using Excel-DNA tool.

The add-in works perfectly when running from Visual Studio, but, when I try to open the packed .xll file from somewhere else, as suggested by Govert (Excel DNA's creator), the plug in ribbon doesn't load. After activating Plug In Error Messages, I get the non-explicit message : The call to GetCustomUI() failed. And that's it.

So I got two questions :

Upvotes: 1

Views: 1073

Answers (2)

XavierA
XavierA

Reputation: 1775

As pointed by @C. Augusto Proiete, I surrounded the GetCustomUI() method override with a try/catch block and logged the exception into a text file. This gave me access to the exception raised when the plug in was starting.

And, bottom line, the issue is that I had an additional JSON configuration file that was not taken into account by the packed XLL, there is seemingly no straightforward method to include it via the DNA file.

The workaround is explained here : set your external file to embedded resource and read it from the Manifest Resource Stream.

In my particular case, I used it into a DI service provider and I build it as follow :

private IServiceProvider BuildServiceProvider()
    {
        var serviceCollection = new ServiceCollection();

        //Configuration
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());

        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "otherconfig.json";
        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader reader = new StreamReader(stream)) {
            string result = reader.ReadToEnd();
            string tempPath = Path.GetTempFileName();
            File.WriteAllText(tempPath, result);
            builder.AddJsonFile(tempPath);
        }

        IConfiguration config = builder.Build();
        serviceCollection.AddSingleton(config);

        //other dependency injection service registration

        return serviceCollection.BuildServiceProvider();
    }

Upvotes: 2

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27898

If you are overriding the method GetCustomUI, put a try...catch on GetCustomUI, and see the exception details.

[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
    public override string GetCustomUI(string RibbonID)
    {
        try
        {
             // ...
        }
        catch(Exception ex)
        {
             MessageBox.Show(ex.ToString());
        }
    }

    // ...
}

Upvotes: 1

Related Questions