Office Excel VSTO工作簿项目Application.ThisWorkbook COMException

时间:2016-03-17 17:06:22

标签: c# .net excel excel-vba comexception vba

我创建了一个新的VSTO Excel工作簿项目,并且只尝试使用" ThisWorkbook"

获取运行宏的工作簿
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

    }
}

代码只是VS创建的默认代码。我只用mWorkbook变量添加了两行。

当我跑步时,我得到了COM异常:

  

发生了System.Runtime.InteropServices.COMException HRESULT:   0x800A03EC

在调试器中,当查看this.Application.ThisWorkbook时,值为:

  

{System.Reflection.TargetInvocationException:异常已经发生   由调用目标抛出。 ---&GT;   System.Runtime.InteropServices.COMException:来自HRESULT的异常:   0x800A03EC ---内部异常堆栈跟踪结束--- at   System.RuntimeType.InvokeDispMethod(String name,BindingFlags   invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,   Int32 culture,String [] namedParameters)at   System.RuntimeType.InvokeMember(String name,BindingFlags   bindingFlags,Binder binder,Object target,Object [] providedArgs,   ParameterModifier []修饰符,CultureInfo文化,String []   namedParams)at   System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1 names)}

我发现唯一接近回答的是&#34;有时这会让其他用户感到沮丧。和&#34;更新.Net&#34;。任何想法接下来要尝试什么?谢谢!

更新 我昨天刚刚安装了Office开发工具。今天,我注意到我可以创建并启动一个项目。如果我关闭它,我不能重新打开,因为版本问题不兼容。与之相关的MS帮助根本没有帮助。我在大约一个月前安装了Windows 10,以及VS社区版,其中包含所有更新。我没有时间为这项任务追逐MS版本的问题,而我现在只想以蛮力的方式构建我的工作簿。有点沮丧,但浪费了太多时间。如果我找到答案,它会稍后回到它并发布。

1 个答案:

答案 0 :(得分:0)

我回答another question也许可以帮助你解决不兼容问题。从&#34; XLS&#34;更改ApplicationType值到&#34; Excel&#34;如果您使用的是VS 2015 Enterprise。