【问题标题】:Passing integers to VSTO 2010 Word Addin将整数传递给 VSTO 2010 Word Addin
【发布时间】:2014-01-10 07:12:24
【问题描述】:

我正在尝试开发 VSTO 2010 Word 插件。它有一个自定义任务窗格,其中包含倒数计时器(用户控件)。想法是,当打开一个 word 文档时,总时间(以小时和分钟为单位)传递给插件,插件又将其传递给倒数计时器控件。当我在 VS2010 中运行插件并传递用户控制两个整数值,即 Addin_Startup 中的小时和分钟时,它工作正常。

现在我正在尝试从 asp.net 页面单击按钮打开一个 word 文档。当我单击按钮时,应该打开一个 word 文档,并且应该将小时和分钟的时间传递给插件,然后插件会将其提供给用户控件,并且计时器应该在给定的时间内运行。

问题是我无法弄清楚如何将两个整数传递给插件以及传递它们的位置。以下是我的代码,有时会给出“灾难性故障错误”,有时会给出“无法将 'System.__ComObject' 类型的 COM 对象转换为接口类型 'MyWordAddin.IAddInUtilities'”

ThisAddin 类

namespace MyWordAddin
{
    public partial class ThisAddIn
    {
        private ctlClock myUserControl;
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
        private int hour, min;
        private AddInUtilities utilities;

        protected override object RequestComAddInAutomationService()
        {
            if (utilities == null)
            {
                utilities = new AddInUtilities();
            }
            return utilities;
        }

        public int Min
        {
            get { return min; }
            set { min = value; }
        }

        public int Hour
        {
            get { return hour; }
            set { hour = value; }
        }


        public Microsoft.Office.Tools.CustomTaskPane MyCustomTaskPane
        {
            get { return myCustomTaskPane; }
            set { myCustomTaskPane = value; }
        }

        public void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //MessageBox.Show("Start Up Called");
            myCustomTaskPane.VisibleChanged += new EventHandler(myCustomTaskPane_VisibleChanged);

        }
        public void setTime(int h, int m)
        {
            Hour = h;
            Min = m;
            myUserControl = new ctlClock(Hour, Min);
            //myUserControl = new ctlClock(0, 1);
            myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl, "Remaining Time");
            myCustomTaskPane.Visible = true;
        }

        private void yCustomTaskPane_VisibleChanged(object sender, System.EventArgs e)
        {
            Globals.Ribbons.ManageTaskPaneRibbon.toggleButton1.Checked = myCustomTaskPane.Visible;
        }

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

AddinUtilities 类

namespace MyWordAddin
{
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IAddInUtilities
    {
        void setAddinTime(int h, int min);
    }

    [ComVisible(true)]
    //[Serializable()]
    [ClassInterface(ClassInterfaceType.None)]
    public class AddInUtilities : StandardOleMarshalObject,IAddInUtilities
    {
        public void setAddinTime(int hour, int min)
        {
            Globals.ThisAddIn.setTime(hour, min);          

        }

   }


}

控制器应用程序尝试打开 word 文档并向插件发送两个整数

namespace ControllerApplication
{
    public class CCWordApp
    {
        private Word._Application oWordApp; // a reference to Word application

        public Word._Application OWordApp
        {
            get { return oWordApp; }
            set { oWordApp = value; }
        }
        private Word.Document oWordDoc;     // a reference to the document

        public Word.Document OWordDoc
        {
            get { return oWordDoc; }
            set { oWordDoc = value; }
        }

        public CCWordApp()
        {
            // activate the interface with the COM object of Microsoft Word
            oWordApp = new Word.Application();
            oWordDoc = new Word.Document();


        }

        // Open a new document
        public void Open()
        {
                object addinName = "MyWordAddIn";
                Microsoft.Office.Core.COMAddIn addin = oWordApp.COMAddIns.Item(addinName);
                IAddInUtilities utils = null;
                    utils = (IAddInUtilities)addin.Object;
                    utils.setAddinTime(0, 8);


            Object oMissing = System.Reflection.Missing.Value;
            oWordApp.Visible = true;
            oWordDoc = oWordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            oWordDoc.Activate();
        }

当我运行控制器应用程序时,在单击开始按钮事件时,有时它会给我“灾难性故障”,有时它给我“无法将类型为“System.__ComObject”的 COM 对象转换为接口类型“MyWordAddin.IAddInUtilities” '”,有时它会卡在最后一行代码上,说“对象实例未传递给对象”。我已经粗体选择了出现错误的代码。我无法弄清楚这里真正的问题是什么以及为什么我不能将两个简单的整数传递给我的插件。肯定有我在以非常错误的方式做的事情。请指导我。

【问题讨论】:

  • ...bold 选择了出现错误的代码 错误在哪里?我没有看到粗体选择。
  • 控制器应用程序的打开函数中出现错误@以下行 utils = (IAddInUtilities)addin.Object; utils.setAddinTime(0, 8);
  • addin.Object 是什么类型的对象?尝试使用GetCOMObjectType using the IDispatch interface to reflect on the COM instance。你能设置一个断点来查看RequestComAddInAutomationService是否被命中?
  • 你试过用ComInterfaceType.InterfaceIsIUnknown声明IAddInUtilities吗?

标签: c# vsto office-interop


【解决方案1】:

我认为您需要覆盖 RequestCOMAddInAutomationService,因为您使用的是 VSTO。

请看这篇文章: How to call the Add-In functions from an external application?

Shared Add-In 使用 COMAddIn.Object 方式公开其功能 到外部应用程序,所以我们的 OnConnection 实现 大致如下所示:

((COMAddIn)AddInInst).Object = new MyAutomationObject();

但是这个方法不能在 VSTO 中使用,因为设置 COMAddIn.Object 仅在 OnConnection 函数和 OnConnection 在 VSTO 项目中不可见。

它给出了一个覆盖 RequestCOMAddInAutomationService() 的示例

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多