C# Office Excel VSTO工作簿项目应用程序。此工作簿异常
我创建了一个新的VSTO Excel工作簿项目,仅尝试使用“ThisWorkbook”获取运行宏的工作簿C# Office Excel VSTO工作簿项目应用程序。此工作簿异常,c#,.net,excel,comexception,vba,C#,.net,Excel,Comexception,Vba,我创建了一个新的VSTO Excel工作簿项目,仅尝试使用“ThisWorkbook”获取运行宏的工作簿 使用系统; 使用System.Collections.Generic; 使用系统数据; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 使用System.Xml.Linq; 使用Microsoft.Office.Tools.Excel; 使用Microsoft.VisualStudio.Tools.Applications.Runtime; 使用
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml.Linq;
使用Microsoft.Office.Tools.Excel;
使用Microsoft.VisualStudio.Tools.Applications.Runtime;
使用Excel=Microsoft.Office.Interop.Excel;
使用Office=Microsoft.Office.Core;
命名空间测试工作簿
{
公共部分课程表1
{
Excel.工作手册;
私有void Sheet1_启动(对象发送方,System.EventArgs e)
{
mWorkBook=this.Application.ThisWorkbook;
}
私有无效表1\u关闭(对象发送器,System.EventArgs e)
{
}
#区域VSTO设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(Sheet1\u启动);
this.Shutdown+=new System.EventHandler(Sheet1\u Shutdown);
}
#端区
}
}
代码只是VS创建的默认代码。我只使用mWorkbook
变量添加了两行代码
运行时,会出现COM异常:
发生System.Runtime.InteropServices.ComeException HRESULT:
0x800A03EC
在调试器中,查看this.Application.ThisWorkbook时,值为:
{System.Reflection.TargetInvocationException:异常已被删除
由调用的目标抛出。-->
System.Runtime.InteropServices.ComeException:来自HRESULT的异常:
0x800A03EC---内部异常堆栈跟踪的结束----at
System.RuntimeType.InvokeDispMethod(字符串名称,BindingFlags
invokeAttr、对象目标、对象[]参数、布尔[]byrefModifiers、,
Int32区域性,字符串[]namedParameters)位于
System.RuntimeType.InvokeMember(字符串名称,BindingFlags
bindingFlags、绑定器绑定器、对象目标、对象[]提供的参数、,
ParameterModifier[]修饰符,CultureInfo区域性,字符串[]
命名参数)在
System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1名称)}
我发现,唯一接近答案的是“有时其他用户不会这样做”和“更新.Net”。你知道下一步该怎么做吗?谢谢
更新
我昨天刚安装了Office开发工具。今天,我注意到我可以创建并启动一个项目。如果我关闭它,我将无法重新打开,因为存在不兼容的版本问题。与之链接的MS帮助根本没有帮助。大约一个月前,我安装了一个干净的Windows 10,VS Community Edition提供了这两个版本的所有更新。我真的没有时间为这项任务追查MS版本的问题,我想现在我只是想用蛮力的方式来构建我的工作簿。有点沮丧,但在这上面浪费了太多时间。如果我找到了答案,我会稍后再返回并发布。我的答案可能会对你有所帮助。如果您使用VS 2015 Enterprise,请将
ApplicationType
值从“XLS”更改为“Excel”。如果您试图获取活动工作簿,可以尝试this.Application.active工作簿
。我相信Application.ThisWorkbook
是用于宏的。我尝试了你的“Application.ActiveWorkbook”,在发布之前遇到了类似的错误。谢谢。您可以尝试的另一件事是附加到应用程序
上的工作簿打开
事件。我以前见过VSTO在Excel准备就绪之前试图获取工作簿时抛出异常。我会尽快尝试。谢谢,谢谢你的帮助!我计划在今年的某个时候回到这一点,并将尝试一下。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
namespace TestWorkbook
{
public partial class Sheet1
{
Excel.Workbook mWorkBook;
private void Sheet1_Startup(object sender, System.EventArgs e)
{
mWorkBook = this.Application.ThisWorkbook;
}
private void Sheet1_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(Sheet1_Startup);
this.Shutdown += new System.EventHandler(Sheet1_Shutdown);
}
#endregion
}
}