0

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.

Community
  • 1
  • 1
MrSnrub
  • 57
  • 7
  • If you are trying to get the active Workbook you could try `this.Application.ActiveWorkbook`. I believe that `Application.ThisWorkbook` is for macros. – Stephen Ross Mar 17 '16 at 17:17
  • I tried your 'Application.ActiveWorkbook' and got a similar error before I posted. Thanks though. – MrSnrub Mar 17 '16 at 19:58
  • Another thing you could try is attaching to the `WorkbookOpen` event on the `Application`. I've seen in the past where VSTO threw an exception when attempting to get hold of the Workbook before Excel was ready. – Stephen Ross Mar 18 '16 at 08:52
  • I'll give that a try, as soon as I get back to this. Thanks. – MrSnrub Mar 18 '16 at 15:06

1 Answers1

0

I answered another question maybe can help you about incompatible. Change ApplicationType value from "XLS" to "Excel" if you are using VS 2015 Enterprise.

Community
  • 1
  • 1
menxin
  • 2,044
  • 1
  • 17
  • 15
  • Thanks for the help! I plan on getting back to this at some point this year and will give it a try. – MrSnrub May 02 '16 at 16:05