[英]Get excel selection content when clicking a ribbon button using exceldna
[英]Invoke Excel Ribbon button for an installed ExcelDna AddIn as part of an integration test
我正在尝试为使用ExcelDna和VSTO自动化开发的Excel插件创建集成测试。 我想测试应用程序的不同功能,但需要将CommandBar用于我们的插件命令栏。
我试图使用Microsoft.Office.Interop.Excel参考和RegisterXLL方法在Excel中注册XLL。
我试图在安装了Office 365的Windows 10盒子上创建测试。
我遇到了几个问题,其中涉及许多可能过时的帖子。
最初,我实例化了Excel应用程序,注册了该插件,然后尝试使用Microsoft.Office.Interop.Excel.SendKeys将击键发送到Excel实例。 虽然最初可以使用断点调试测试时使它工作,但在运行测试时却无法工作。 有多个帖子表明,在Windows 10上,Office现在是通用应用程序,并且因为SendKeys违反了UAC,SendKeys将不再起作用,并且Win32 API调用向应用程序发送密钥的调用同样不起作用,因为Office是通用应用程序。
我的第二次尝试是尝试通过VSTO自动化代码调用Commandbar及其菜单项,而我似乎找不到任何指示如何在不同应用程序域中调用Commandbar功能的帖子。
此外,当我关闭Excel实例时,我注意到即使使用下面的代码杀死了excel应用程序,也始终有一个Excel进程在运行。
void CloseApp()
{
xlApp.Quit();
xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
我对如何实现这一目标感到困惑。 是否有人可以指向解决这些仍然有效的问题的帖子?
关于结束,您可能希望使用GC调用中的COM调用来拆分代码,以确保调试器不会使局部变量保持活动状态(并防止GC运行和释放COM对象。因此,您需要遵循此步骤图案:
using System;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
namespace TestCsCom
{
class Program
{
static void Main(string[] args)
{
// NOTE: Don't call Excel objects in here...
// Debugger would keep alive until end, preventing GC cleanup
// Call a separate function that talks to Excel
DoTheWork();
// Now let the GC clean up (repeat, until no more)
do
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
while (Marshal.AreComObjectsAvailableForCleanup());
}
static void DoTheWork()
{
Application app = new Application();
Workbook book = app.Workbooks.Add();
Worksheet worksheet = book.Worksheets["Sheet1"];
app.Visible = true;
for (int i = 1; i <= 10; i++) {
worksheet.Cells.Range["A" + i].Value = "Hello";
}
book.Save();
book.Close();
app.Quit();
// NOTE: No calls the Marshal.ReleaseComObject() are ever needed
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.