我正在尝试使用 VBA 创建一个新的 Excel 实例:
Set XlApp = New Excel.Application
问题是这个新的 Excel 实例不会加载我正常打开 Excel 时加载的所有插件...... Excel 应用程序对象中是否有任何内容用于加载所有用户指定的插件?
我不是要加载特定的加载项,而是让新的 Excel 应用程序的行为就像用户自己打开它一样,所以我真的在寻找通常加载的所有用户选择的加载项的列表打开 Excel 时。
我正在尝试使用 VBA 创建一个新的 Excel 实例:
Set XlApp = New Excel.Application
问题是这个新的 Excel 实例不会加载我正常打开 Excel 时加载的所有插件...... Excel 应用程序对象中是否有任何内容用于加载所有用户指定的插件?
我不是要加载特定的加载项,而是让新的 Excel 应用程序的行为就像用户自己打开它一样,所以我真的在寻找通常加载的所有用户选择的加载项的列表打开 Excel 时。
我再次查看了这个问题,Application.Addins 集合似乎在“工具”->“插件”菜单中列出了所有插件,并带有一个布尔值,说明是否安装了插件。所以现在似乎对我有用的是循环遍历所有插件,如果 .Installed = true 然后我将 .Installed 设置为 False 并返回 True,这似乎可以正确加载我的插件。
Function ReloadXLAddins(TheXLApp As Excel.Application) As Boolean
Dim CurrAddin As Excel.AddIn
For Each CurrAddin In TheXLApp.AddIns
If CurrAddin.Installed Then
CurrAddin.Installed = False
CurrAddin.Installed = True
End If
Next CurrAddin
End Function
不幸的是, usingCreateObject("Excel.Application")
的结果与 using 相同。New Excel.Application
Application.Addins.Add(string fileName)
您必须使用该方法通过文件路径和名称单独加载您需要的插件。
我将把这个答案留给其他遇到这个问题但使用 JavaScript 的人。
一点背景知识... 在我的公司,我们有一个使用 JavaScript 启动 Excel 并动态生成电子表格的 3rd 方网络应用程序。我们还有一个 Excel 加载项,它覆盖了“保存”按钮的行为。该插件让您可以选择将文件保存在本地或我们的在线文档管理系统中。
After we upgraded to Windows 7 and Office 2010, we noticed a problem with our spreadsheet-generating web app. When JavaScript generated a spreadsheet in Excel, suddenly the Save button no longer worked. You would click save and nothing happened.
Using the other answers here I was able to construct a solution in JavaScript. Essentially we would create the Excel Application object in memory, then reload a specific add-in to get our save button behavior back. Here's a simplified version of our fix:
function GenerateSpreadsheet()
{
var ExcelApp = getExcel();
if (ExcelApp == null){ return; }
reloadAddIn(ExcelApp);
ExcelApp.WorkBooks.Add;
ExcelApp.Visible = true;
sheet = ExcelApp.ActiveSheet;
var now = new Date();
ExcelApp.Cells(1,1).value = 'This is an auto-generated spreadsheet, created using Javascript and ActiveX in Internet Explorer';
ExcelApp.ActiveSheet.Columns("A:IV").EntireColumn.AutoFit;
ExcelApp.ActiveSheet.Rows("1:65536").EntireRow.AutoFit;
ExcelApp.ActiveSheet.Range("A1").Select;
ExcelApp = null;
}
function getExcel() {
try {
return new ActiveXObject("Excel.Application");
} catch(e) {
alert("Unable to open Excel. Please check your security settings.");
return null;
}
}
function reloadAddIn(ExcelApp) {
// Fixes problem with save button not working in Excel,
// by reloading the add-in responsible for the custom save button behavior
try {
ExcelApp.AddIns2.Item("AddInName").Installed = false;
ExcelApp.AddIns2.Item("AddInName").Installed = true;
} catch (e) { }
}