我正在尝试开发 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'”和有时它卡在最后一行代码上,上面写着“对象实例未传递给对象”。我已经粗体选择了出现错误的代码。我无法弄清楚这里真正的问题是什么以及为什么我不能将两个简单的整数传递给我的插件。肯定有我在以非常错误的方式做的事情。请指导我。