I created a new VSTO Excel Workbook Project and only attempt to get the workbook in which the macro is running using "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
}
}
The code is just the default created by VS. I only added the two lines with the mWorkbook
variable.
When I run, I get the COM exception:
System.Runtime.InteropServices.COMException occured HRESULT: 0x800A03EC
In the debugger, when looking at this.Application.ThisWorkbook the value is:
{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)}
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!
Update 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.