如果满足以下条件,在调用某个 Excel 方法时会收到此错误:
- 该方法需要一个 LCID(区域设置标识符)。
- 运行的是英语版本的 Excel。但是,计算机的区域设置是针对非英语语言配置的。
如果客户端计算机运行的是英语版本的 Excel 并且当前用户的区域设置配置为英语之外的某个语言,则 Excel 将尝试查找针对所配置语言的语言包。如果没有找到所需语言包,则会报告错误。
两种解决方案:
《一》安装适用于您的 Office 版本的多语言用户界面包。
《二》加上ExcelApp.Visible[LOCALE_USER_DEFAULT]:=true; 这一句。
《三》使用 InvokeMember 执行 Excel 方法或属性,以便可以为调用指定 CultureInfo。例如,下面的代码演示如何使用“en-US”作为 CultureInfo 的值来调用 Workbooks 对象的 Add 方法:
Dim oApp As New Excel.Application() oApp.Visible = True oApp.UserControl = True Dim oBooks As Object = oApp.Workbooks Dim ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US") oBooks.GetType().InvokeMember("Add", Reflection.BindingFlags.InvokeMethod, Nothing, oBooks, Nothing, ci)
《四》在调用 Excel 方法之前设置 CultureInfo。例如:
Dim oApp As New Excel.Application() oApp.Visible = True oApp.UserControl = True Dim oldCI As System.Globalization.CultureInfo = _ System.Threading.Thread.CurrentThread.CurrentCulture System.Threading.Thread.CurrentThread.CurrentCulture = _ New System.Globalization.CultureInfo("en-US") oApp.Workbooks.Add() System.Threading.Thread.CurrentThread.CurrentCulture = oldCI