VSTO AddIn中访问Globals.ThisAddIn.Application的异常

问题描述 投票:0回答:1

我正在尝试使用C#创建MS Office VSTO加载项。插件包含一个功能区,我通过VS19中的功能区设计器添加了该功能区。

我未修改ThisAddin.cs文件:

namespace MyAddIn
{
    public partial class ThisAddIn
    {

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO 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(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

在功能区中,我添加了一个按钮,并编写了一些单击时要执行的代码:

namespace MyAddIn
{
    public partial class my_ribbon
    {
        private void my_ribbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void btn_myButton_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show(Globals.ThisAddIn.Application.ThisWorkbook.FullName);
        }
    }
}

但是,每当我单击按钮时,C#都会向我抛出以下异常:

System.Runtime.InteropServices.COMException
  HResult=0x800A03EC
  Message=Ausnahme von HRESULT: 0x800A03EC
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

[调试时,我看到Globals.ThisWorkbook.Application引发了错误。我发现0x800A03EC是“找不到名称”的错误代码(根据this)。但是,一旦打开文件,当我单击按钮时,应该有一个应用程序对象以及全名。

我也看了this guide,但这并没有太大帮助,因为他们也成功使用了Globals.ThisWorkbook.Application

您对如何解决此问题有任何建议吗? (是否需要更多初始化代码,等等?)>

我正在尝试使用C#创建MS Office VSTO加载项。插件包含一个功能区,我通过VS19中的功能区设计器添加了该功能区。我未修改ThisAddin.cs文件:名称空间MyAddIn {...

c# vsto excel-addins
1个回答
0
投票

[@Cindy Meister]指向正确的方向:

© www.soinside.com 2019 - 2024. All rights reserved.