- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在遍历包含多个 excel 文件的目录,并试图一次刷新一个 excel 文件。我不断收到此错误,这表明刷新操作仍在文件 A 上运行,例如,文件 B 正在尝试启动刷新操作。循环速度太快,不知何故,我必须等待文件 A 上的先前刷新操作完成,然后才能开始刷新文件 B。
Unhandled Exception: System.Runtime.InteropServices.COMException: The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)) at Microsoft.Office.Interop.Excel._Workbook.RefreshAll() at RefreshExcelFiles.Program.RefreshFile(String fileName)
这是我的代码。在循环中开始处理新文件之前,如何等待刷新操作完成?或者,在开始新文件之前等待对 wb 对象的 marshal 释放操作完成?
using System;
using System.Configuration;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
namespace RefreshExcelFiles
{
internal class Program
{
private static string _fileDirectory;
private static Application _excelApp;
private static void Main(string[] args)
{
_fileDirectory = ConfigurationManager.AppSettings["FileDirectory"];
_excelApp = new Application();
_excelApp.DisplayAlerts = false;
Console.WriteLine("starting to refresh files");
int i = 0;
int total = Directory.GetFiles(_fileDirectory).Length;
foreach (string file in Directory.GetFiles(_fileDirectory))
{
i += 1;
Console.WriteLine("File " + file + " " + i.ToString() + "/" + total.ToString());
RefreshFile(file);
}
_excelApp.Quit();
Marshal.FinalReleaseComObject(_excelApp);
Console.WriteLine("press any key to exit");
Console.ReadLine();
}
private static void RefreshFile(string fileName)
{
_Workbook wb = _excelApp.Workbooks.Open(fileName, false);
wb.RefreshAll();
wb.Save();
wb.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(wb);
}
}
}
最佳答案
我找到了问题的答案。我需要 implement IMessageFilter RetryRejectedCall .对于使用 IMessageFilter::RetryRejectedCall 的 C# 和 VB.NET 代码示例,see this blog post (wayback machine archived link) .
如果您自己没有注册 MessageFilter(通过调用 CoRegisterMessageFilter),您将获得默认行为,如果调用失败被拒绝。 .Net 将故障 HRESULT 转换为异常。为了处理您尝试调用时服务器忙的可能性,您需要在您的客户端代码中实现 IMessageFilter::RetryRejectedCall 并注册消息过滤器。在大多数情况下,您只需要等待几秒钟,然后重试调用 - 通常,这段时间足以让 Word 完成它正在做的任何事情,以便它可以处理你的调用。
关于C# Excel 互操作 - 如何测试互操作对象是否仍在工作并执行任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467510/
我试图找出如何访问 ClojureScript 中的 Javascript 对象属性。如果我事先知道属性(property)的名称,那就很容易了。获取foo.bar我只是做 (.-bar foo) I
我是 .NET 的新手。我想制作一个控制台应用程序,将 .pptx 文件转换为 .wmv。我已经使用 powerpoint interop 做到了这一点。但我有一些问题。首先,如果我构建应用程序并将其
我计划将我的许可系统的核心转移到 C++,但我仍然更喜欢使用 .NET 前端进行设计。无论如何,我刚刚读完互操作功能,并决定对其进行测试。问题是,它对我来说只是花花公子,但对任何其他用户都不起作用。我
系统管理员正在编写一些常用的管理 Power Shell 脚本。主要用于 AD 管理(更新交易所详细信息、在安全组中移动人员等) 我想使用 C# 中的这些脚本(我打算将其编写为库,供网站使用)。 我看
我想在我的应用程序中使用 COM 对象。 如何确保对象已在机器中注册? 我找到的唯一解决方案(也是 on SO)是在初始化周围使用 try-catch block : try { Foo.Ba
这个问题在这里已经有了答案: How to call Java code from C#? (4 个答案) 关闭 10 个月前。 您能给我一些关于使 C# 代码和 Java 代码互操作的建议吗?让我
我正在使用 Microsoft.Office.Interop.Excel 从 C# 创建一个 Excel 工作表,但我无法按照用户想要的方式获取页脚。 1) 如何将页脚文本加粗? 2)如何将页码放在页
我正在使用 F# 和 Excel Interop 将数据输出到 Excel 电子表格。我的第一种方法是单独设置每个单元格: worksheet.Range(range1).Value2 <- "=su
与来自 Silverlight 的 COM 控件进行交互的选项有哪些? 在我的特定项目中,我有一个旧的 ActiveX 身份验证控件,我想在我的 Silverlight 应用程序中利用它。没有太多无聊
我需要针对以下场景的一些建议: 我的 Uni 组有一个巨大的 SVN 存储库。我实际上对整个项目的子目录感兴趣(例如/trunk/projects/my_project)- 不知道它是否真的与 SVN
我有一个 .NET 程序集,它是通过 COM Interop 从 Delphi 主机调用的。据我所知,.NET 代码中任何未处理的异常都将由 .net com 互操作框架处理,并且相应的 HRESUL
我有一个程序可以在单击按钮时创建两个 pdf 文件。它在 WinForms 中使用 Microsoft Office 互操作,文件创建过程如下; 用户在程序中工作 点击按钮 程序根据一个带有书签的模板
我有一个第三方 DLL 用 Delphi“a.dll”(无源代码)编写。 这个 DLL 有一个带有这个签名的方法。 function GetAny(pFileName: String): String
想知道是否有人成功使用 JDEdwards XMLInterop 功能。我已经使用它有一段时间了(使用一个简单的 PInvoke,稍后将发布代码)。我正在寻找是否有更好和/或更强大的方法。 谢谢。 最
我正在尝试让 javafx2 与 Clojure 一起使用 - 在实现像 DoubleBinding 这样的抽象类时,我不确定 Clojure 中 super.bind(moo) 的等价物是什么。我正
有人有使用 Firebird 与 .NET 框架互操作的经验吗?如果有,进展如何? 最佳答案 我在商业桌面应用程序中使用了 firebird。 它的性能很好,直到您处理返回大型结果集的查询。在这些情况
我正在 Excel 工作表中复制并插入行,如下所示: while (rowsToAdd > 0) { // copy the existing row insert
我无法确保正确销毁托管窗口。 我有一个 HwndHost 派生类,正在 TabControl 中显示(尽管这可能不相关)。我试图在选项卡关闭时销毁托管内容(而不是在包含的窗口关闭时)。 我目前拥有 m
我有两个 python 类,它们都可以使用 COM 互操作机制独立于 VBA 编写脚本。但我希望一个能够以父子模式或工厂模式控制另一个的创建。 我已经尝试过,但无法开始工作,我已将其提炼为下面的 MC
我们在我们编写的 C# dll(程序集 A)中使用 Microsoft 提供的 COM DLL (dsofile.dll)。为了避免必须注册 COM dll,我已将对 dsofile.dll 的引用的
我是一名优秀的程序员,十分优秀!