Can't load xll programmatically

前端 未结 5 491
青春惊慌失措
青春惊慌失措 2021-01-14 12:40

I\'m trying to automating some tests for an Excel add-in, which is in xll form. I have some problem in loading the xll. I\'m writing it in C# and my code looks like this:

相关标签:
5条回答
  • 2021-01-14 13:08

    Yes, the ChDir command can be important. It helps Windows find any DLLs that whatever.xll depends on. The reason it doesn't solve your problem is that FileSystem.ChDir() changes the working directory for your test program, not for Excel.

    Not a wholeheckofalot you can do. Deploying the xll do a directory that's on the system's PATH will solve it. A pragmatic solution is to just run that macro.

    0 讨论(0)
  • 2021-01-14 13:13

    I was facing issues with loading user defined function [UDF] in VBA code. Was able to load xll file but wasn't able to call.

    Following code successfully loads XLL files and let's call user defined function from VBA code. There may be redudendent instruction in this module but it works!

    Sub InstallAddIn()
    
    On Error GoTo ErrorHandle
    
    Application.DefaultFilePath = "D:\\MyFolder"
    
    Application.RegisterXLL ("MyXLLFileName.xll")
    
    Set AI = AddIns.Add(Filename:="D:\MyFolder\MyXLLFileName.xll")
    
        If AddIns("MyXLLFileName").Installed Then
           LogInformation ("My XLL is installed")
        Else
           LogInformation ("My XLL is NOT installed")
        End If
    Exit Sub
    
    ErrorHandle:
    
    LogInformation ("------------------------") 'Logging function that I have written. Not a std api
    
    LogInformation (Err.HelpFile)
    
    LogInformation (Err.HelpContext)
    
    LogInformation (Err.Description)
    
    LogInformation ("Error in InstallAddIn module")
    
    LogInformation ("------------------------")
    
    End
    
    End Sub
    
    0 讨论(0)
  • 2021-01-14 13:14

    I know this is not a direct answer to your question, but you may want to look at using VSTO within Visual Studio. VSTO automates a lot of these types of issues. The version in VS 2010 is so much better than what they used to have and you can built application level add-ins as opposed to just document level add-ins. If you need user defined functions you can use a COM add-in as described here:

    http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx

    It was originally based on this article:

    http://blogs.msdn.com/eric_carter/archive/2004/12/01/273127.aspx

    We use a combination of VSTO for our main application, and the COM add-in for user defined functions. What's nice about that is that they are loaded in the same app domain so they can talk to each other.

    0 讨论(0)
  • 2021-01-14 13:31

    Thanks for all the suggestions.

    I solved the problem by changing the default file path for the Excel application.

    Application.xlApp = new ApplicationClass();
    xlApp.DefaultFilePath = "C:\\SomePath";
    xlApp.RegisterXLL("Whatever.xll");
    
    0 讨论(0)
  • 2021-01-14 13:31

    A proper solution would be :

    1- save the current directory with

     string CurrentDir =   Directory.GetCurrentDirectory()
    

    2- then you use

      Directory.SetCurrentDirectory(dirXll);
    

    where dirXll is the location of your xll (cf GetExecutingAssembly()) .

    3- Load your xll (RegisterXLL)

    4- and finaly use CurrentDir to set the current directory back to its original location.

    Don't forget to add all dlls that you xll is relyong on in the same folder as your xll.

    0 讨论(0)
提交回复
热议问题