无法以编程方式加载 xll

发布于 2024-09-08 12:03:45 字数 813 浏览 5 评论 0原文

我正在尝试自动化 Excel 加载项的一些测试,该加载项采用 xll 形式。我在加载 xll 时遇到一些问题。我用 C# 编写它,我的代码如下所示:


using Microsoft.Office.Interop.Excel;

Application xlApp;
Workbook xlWorkBook;
Worksheet xlWorkSheet;

// create application, open workbook, etc ...
// now try to register xll
xlApp.RegisterXLL("C:\\SomePath\\Whatever.xll");

但是,这总是返回 false。我尝试通过录制宏来手动加载 xll 来查看 Excel 秘密执行的操作。宏看起来像:


Sub Macro1()
ChDir "C:\SomePath"
Application.RegisterXLL Filename:= _
"C:\SomePath\Whatever.xll"
End Sub

唯一的区别似乎是 ChDir,所以我将代码更改为:


FileSystem.ChDir("C:\\SomePath");
xlApp.RegisterXLL("C:\\SomePath\\Whatever.xll");

但它仍然不起作用。另一个奇怪的事情是,当我在 RegisterXLL 行之前放置一个断点并首先手动加载 xll 时,RegisterXLL 方法将返回 true。但否则,它将返回 false。

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:


using Microsoft.Office.Interop.Excel;

Application xlApp;
Workbook xlWorkBook;
Worksheet xlWorkSheet;

// create application, open workbook, etc ...
// now try to register xll
xlApp.RegisterXLL("C:\\SomePath\\Whatever.xll");

However, this always return false. I try to see what Excel secretly does when I load the xll manually by recording the macro. The macro looks like:


Sub Macro1()
ChDir "C:\SomePath"
Application.RegisterXLL Filename:= _
"C:\SomePath\Whatever.xll"
End Sub

The only difference seems to be the ChDir, so I changed my code to:


FileSystem.ChDir("C:\\SomePath");
xlApp.RegisterXLL("C:\\SomePath\\Whatever.xll");

But it still doesn't work. Another odd thing is when I put a breakpoint before the RegisterXLL line and load the xll manually first, the RegisterXLL method will return true. But otherwise, it will return false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情场扛把子 2024-09-15 12:03:45

感谢您的所有建议。

我通过更改 Excel 应用程序的默认文件路径解决了该问题。

Application.xlApp = new ApplicationClass();
xlApp.DefaultFilePath = "C:\\SomePath";
xlApp.RegisterXLL("Whatever.xll");

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");
我要还你自由 2024-09-15 12:03:45

是的,ChDir 命令可能很重要。它可以帮助Windows找到whatever.xll所依赖的任何DLL。它不能解决您的问题的原因是 FileSystem.ChDir() 更改了您的测试程序的工作目录,而不是 Excel 的工作目录。

你根本做不到。将 xll do 部署到系统 PATH 上的目录即可解决该问题。一个务实的解决方案是只运行该宏。

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.

梦在深巷 2024-09-15 12:03:45

我知道这不是您问题的直接答案,但您可能想看看在 Visual Studio 中使用 VSTO。 VSTO 可以自动处理许多此类问题。 VS 2010 中的版本比以前的版本要好得多,您可以构建应用程序级加载项,而不仅仅是文档级加载项。如果您需要用户定义的函数,您可以使用 COM 加载项,如下所述:

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

它最初基于这篇文章:

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

我们将 VSTO 组合用于我们的主应用程序,并使用 COM 插件来实现用户定义的功能。这样做的好处是它们被加载到同一个应用程序域中,因此它们可以相互通信。

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.

花心好男孩 2024-09-15 12:03:45

正确的解决方案是:

1- 用 2- 保存当前目录,

 string CurrentDir =   Directory.GetCurrentDirectory()

然后使用

  Directory.SetCurrentDirectory(dirXll);

dirXll 是 xll 的位置(参见 GetExecutingAssembly())。

3- 加载 xll (RegisterXLL)

4- 最后使用 CurrentDir 将当前目录设置回其原始位置。

不要忘记将您的 xll 依赖的所有 dll 添加到与您的 xll 相同的文件夹中。

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.

云归处 2024-09-15 12:03:45

我在 VBA 代码中加载用户定义函数 [UDF] 时遇到问题。能够加载 xll 文件但无法调用。

以下代码成功加载 XLL 文件,让我们从 VBA 代码调用用户定义的函数。该模块中可能有重复的指令,但它有效!

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文