title: Office Excel VSTO Workbook Project Application.ThisWorkbook COMException
body: <p>I created a new VSTO Excel Workbook Project and only attempt to get the workbook in which the macro is running using "ThisWorkbook"</p>
  
  <pre><code>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
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InternalStartup()
          {
              this.Startup += new System.EventHandler(Sheet1_Startup);
              this.Shutdown += new System.EventHandler(Sheet1_Shutdown);
          }
  
          #endregion
  
      }
  }
  </code></pre>
  
  <p>The code is just the default created by VS. I only added the two lines with the <code>mWorkbook</code> variable.</p>
  
  <p>When I run, I get the COM exception: </p>
  
  <blockquote>
    <p>System.Runtime.InteropServices.COMException occured HRESULT:
    0x800A03EC</p>
  </blockquote>
  
  <p>In the debugger, when looking at this.Application.ThisWorkbook the value is:</p>
  
  <blockquote>
    <p>{System.Reflection.TargetInvocationException: Exception has been
    thrown by the target of an invocation. --->
    System.Runtime.InteropServices.COMException: Exception from HRESULT:
    0x800A03EC    --- End of inner exception stack trace ---    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[] modifiers, CultureInfo culture, String[]
    namedParams)    at
    System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1 names)}</p>
  </blockquote>
  
  <p>The only thing close to answer that I have found is "sometimes this goes away for other users" and "update .Net". Any ideas what to try next? Thanks!</p>
  
  <p><strong>Update</strong>
  I just installed the Office Development tools yesterday. Today, I noticed that I can create and start a project. If I close it, I cannot reopen because of an incompatible version issue. The MS help linked to that was no help at all. I have a clean install of Windows 10 from a month or so ago, and VS Community Edition with all updates for both. I don't really have time to chase down a MS version issues for this task, and I'm just going to build my workbook the brute force way for now I guess. Slightly frustrated, but wasted too much time on this. Will circle back to it later and post if I find an answer.</p>