我正在尝试使用Visual Studio Office Developer Tools编写我的第一个Excel加载项。我按照这个例子:https://msdn.microsoft.com/en-us/library/cc668205.aspx。因此我的代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddIn1
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Application_WorkbookBeforeSave(Microsoft.Office.Interop.Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
Excel.Range firstRow = activeWorksheet.get_Range("A1");
firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
Excel.Range newFirstRow = activeWorksheet.get_Range("A1");
newFirstRow.Value2 = "This text was added by using code";
}
// VSTO-generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
}
}
当我构建程序并启动Excel时,Excel会发出无法找到或加载加载项的消息以及以下代码:
Exception from HRESULT: 0x8004063E
****************************
System.Runtime.InteropServices.COMException (0x8004063E): Exception from HRESULT: 0x8004063E
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.CreateCustomizationDomainInternal(String solutionLocation, String manifestName, String documentName, Boolean showUIDuringDeployment, IntPtr hostServiceProvider, Boolean useFastPath, IntPtr& executor)
at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IDomainCreator.CreateCustomizationDomain(String solutionLocation, String manifestName, String documentName, Boolean showUIDuringDeployment, IntPtr hostServiceProvider, IntPtr& executor)
我猜问题不是在代码中,而是在Excel尝试加载加载项的方式。我已经检查了所有提到的应用程序级项目on this Microsoft page和注册表项from this Microsoft page的文件。 (有趣的是,注册表项在32位的密钥中虽然我使用了64位。我尝试手动将它们添加到64位密钥,但这没有帮助。)VSTO加载项在Excel中激活。
操作系统是具有用户权限的Windows 7 64位,我使用Excel 2013和Visual Studio 2015。