1

I have a C# method that accesses the Excel API that's defined in the office.dll that gets installed in the GAC with MS office.

This method fails with a FileNotFoundException when being called on a computer that doesn't have MS office installed even if I have a statement that avoids calling the api.

ie. This code throws an exception on an office-free computer, I'm guessing during type loading.

void TestStuff() 
{
    try 
    {
        if (machineHasNoExcel) return;
        Excel.Application.Stuff();
    } catch() { }
}

How can I avoid that exception? Can I make the typeloader not throw and trust that the code is not going to call the missing API? I really would prefer to avoid late binding.

edeboursetty
  • 5,669
  • 2
  • 40
  • 67
  • You can catch the exception yourself and return. – Robert Harvey Apr 29 '19 at 15:17
  • 1
    Ah, I see you're already doing it. If you don't want to do late binding, I guess this is the best you're going to get. – Robert Harvey Apr 29 '19 at 15:19
  • @RobertHarvey: sorry, I forgot to add the try catch block doesn't work. This is because the exception is thrown during type loading. – edeboursetty Apr 29 '19 at 15:19
  • During the loading of what type? `machineHasNoExcel` is a *type*? – Robert Harvey Apr 29 '19 at 15:20
  • Are you sure you even need to use Office Interop? What's the purpose? Would a library that can directly manipulate Excel documents without relying on COM interop be more suitable? – mason Apr 29 '19 at 15:20
  • the loading of the class – edeboursetty Apr 29 '19 at 15:20
  • 4
    Before this code can execute it must first be just-in-time compiled. That is going to fail, no way for the jitter to know what "Excel" means when the interop assembly is missing. That exception is raised at an awkward time, it happens in the caller of this method. Or the caller of the caller or the caller of the caller of the caller in the Release build. There is a much simpler solution for this, just don't create a dependency on the interop assembly. https://stackoverflow.com/a/21018418/17034 – Hans Passant Apr 29 '19 at 15:21
  • @mason: yes I do – edeboursetty Apr 29 '19 at 15:21
  • @d--b Why? What feature prevents you from using say, [EPPlus](https://github.com/JanKallman/EPPlus)? – mason Apr 29 '19 at 15:22
  • Or the Open XML Library? There are plenty of options that can be better than Interop. – Robert Harvey Apr 29 '19 at 15:24
  • @mason: this method is a log sink. When called by a method that lives in an Excel process, I want to know the path of the workbook that's opened. But if it's in IIS where Excel is unknown, I don't want it to fail. – edeboursetty Apr 29 '19 at 15:25
  • Eek. You're trying to do Interop on an ASP.NET server? – Robert Harvey Apr 29 '19 at 15:25
  • Alright, well there's only one reason that the code you posted can fail: `machineHasNoExcel` is returning the wrong result. – Robert Harvey Apr 29 '19 at 15:27
  • Have you confirmed that you're throwing on the line `Excel.Application.Stuff();`? – Robert Harvey Apr 29 '19 at 15:28
  • @RobertHarvey: see Hans's comment. It's throwing during JIT compilation, so before the code actually gets called. I can probably wrap it in another function so that the code can fail then and I get to catch it. – edeboursetty Apr 29 '19 at 15:38

0 Answers0