如何使用sendMessage()从VB makro到c#Excel工作簿Office插件
使用Excel-2010、Visual Studio-2013 Professional、 我正试图从VisualBasic makro向c#Office插件(更准确地说是Excel 2010工作簿Office插件,[不是ThisAdd插件])发送(或发布)一条消息。你是怎么做到的??VisualBasic代码应该是什么样子 按照与的类似方法,我无法按照链接解决方案中的说明使以下覆盖生效:如何使用sendMessage()从VB makro到c#Excel工作簿Office插件,c#,vba,visual-studio-2013,excel-addins,C#,Vba,Visual Studio 2013,Excel Addins,使用Excel-2010、Visual Studio-2013 Professional、 我正试图从VisualBasic makro向c#Office插件(更准确地说是Excel 2010工作簿Office插件,[不是ThisAdd插件])发送(或发布)一条消息。你是怎么做到的??VisualBasic代码应该是什么样子 按照与的类似方法,我无法按照链接解决方案中的说明使以下覆盖生效: protected override object RequestComAddInAutomationSe
protected override object RequestComAddInAutomationService()
在我看来,这种方法只适用于c#Office加载项(它是ThisAdd-In而不是工作簿加载项)。我在Visual Studio中选择以下项目选项(在“新建-->项目-->Visual c#…”下):“Excel 2010工作簿”Office加载项:
在我看来,有太多不同的插件,很难理解
我的第一个问题仍然是:
如何将VisualBasic Makro的信息发送到此Excel-2010工作簿???
Visual Basic代码应该是什么样子的
这是我的c#部分(在ThisWorkbook.cs中)(由于“受保护的覆盖问题”,不确定是否正确)
感谢您的帮助 你所展示的代码看起来像是来自模板本身,你自己编写了多少代码,到目前为止你做了哪些尝试?我在上面的问题中添加了VB代码-而且,我添加了我自己获取的大部分代码-请看一下我在上面的问题中添加的VB代码。。。不幸的是,VB代码在
Application.COMAddIns(“…”)
上抛出了一个异常,这个VB代码应该是什么样子的??还有,上面的c代码是否正确??
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;
using System.Runtime.InteropServices;
using stdole;
namespace ExcelConfig
{
/// <summary>Interface for the COM-object, this is what the VBA actually will see/work against</summary>
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMacroMessages : IDispatch
{
void SendMessage(string message);
}
/// <summary>Implementation of the COM-interface</summary>
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class MacroMessages : IMacroMessages
{
public void SendMessage(string message)
{
// Our implementation in VSTO
MessageBox.Show(message);
}
}
public partial class ThisWorkbook
{
public static MacroMessages MacroMessages { get; set; }
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Here, the "protected override" did not work as in the ThisAddIn link
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public object RequestComAddInAutomationService()
{
return MacroMessages ?? (MacroMessages = new MacroMessages());
}
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
}
private void ThisWorkbook_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(ThisWorkbook_Startup);
this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
}
#endregion
}
}
Sub sendMesssageToActiveWindow()
Dim addin As COMAddIn
Dim comObject As Object
' Load the VSTO addin
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
' Here is where an error occurrs currently
' How to solve this ????
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Set addin = Application.COMAddIns("ExcelConfig.xlsm")
' Fetch the COM object, unfortunatly we´re not able to get any intellisense here as it´s loaded runtime
Set comObject = addin.Object
' Invoke the method
comObject.SendMessage "This is the message we pass to the method"
End Sub