gpt4 book ai didi

c# - 将整数传递给 VSTO 2010 Word 插件

转载 作者:太空狗 更新时间:2023-10-29 19:48:02 25 4
gpt4 key购买 nike

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

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

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

这个插件类

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 文档并向插件发送两个整数的 Controller 应用程序

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();
}

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

最佳答案

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

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

Shared Add-In uses the COMAddIn.Object manner to expose its functions to the external applications, so our implementation of OnConnection will roughly look like this:

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

However this method cannot be used in VSTO because setting COMAddIn.Object is only permitted during OnConnection function and the OnConnection is not visible in VSTO project.

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

希望对您有所帮助!

关于c# - 将整数传递给 VSTO 2010 Word 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13046142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com