当以编程方式实例化Excel时加载加载项

我正在尝试使用VBA创build一个新的Excel实例:

Set XlApp = New Excel.Application

问题是,这个新的Excel实例不会加载所有加载,当我正常打开Excel时加载…有什么在Excel应用程序对象中加载所有用户指定的加载项?

我不是要加载一个特定的加载项,而是让新的Excel应用程序的行为就像用户打开它自己,所以我真的在寻找一个所有用户select的加载项列表,通常加载打开Excel时。

我再次查看了这个问题,并且Application.Addins集合似乎具有Tools-> Addins菜单中列出的所有插件,其布尔值指出是否安装了插件。 所以现在似乎为我工作的是循环所有的插件,如果.Installed = 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 

不幸的是,使用CreateObject("Excel.Application")与使用New Excel.Application结果相同。

你将不得不使用Application.Addins.Add(string fileName)方法,通过文件path和名称单独加载你需要的Addins。

我将这个答案留给这个问题的其他人,但是使用JavaScript。

一个小背景…在我的公司,我们有一个第三方的Web应用程序,使用JavaScript来启动Excel,并在飞行中生成一个电子表格。 我们也有一个Excel加载项来覆盖保存button的行为。 加载项允许您在本地或在线文档pipe理系统中保存文件。

在升级到Windows 7和Office 2010之后,我们注意到我们的电子表格生成Web应用程序出现问题。 当JavaScript在Excel中生成电子表格时,突然间保存button不再有效。 你会点击保存并没有发生任何事情。

在这里使用其他答案,我能够在JavaScript中构build一个解决scheme。 本质上,我们将在内存中创buildExcel应用程序对象,然后重新加载特定的加载项以恢复保存button行为。 以下是我们修复的简化版本:

 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) { } } 

尝试:

Set XlApp = CreateObject("Excel.Application")