VSTO开发Excel自定义公式的问题

zqsntws 2011-12-27 10:20:35
我目前在用VSTO开发Excel的插件,里面有自定义菜单,也有些自定义公式。碰到个问题:在自定义公式里得不到当前的Application。Globals.ThisAddIn为null,不知道为什么 ;(
...全文
293 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqsntws 2011-12-27
  • 打赏
  • 举报
回复
sinitek123:你这个是共享的外接程序模式吧。
sinitek123 2011-12-27
  • 打赏
  • 举报
回复
using MsExcel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;


public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
app = (MsExcel.Application)application;

}


MsExcel.Application excelapp = (MsExcel.Application)app;
sinitek123 2011-12-27
  • 打赏
  • 举报
回复
using MsExcel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;


public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
app = (MsExcel.Application)application;

}


MsExcel.Application excelapp = (MsExcel.Application)app;
zqsntws 2011-12-27
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace UDFTest
{
[Guid("05DE8F39-D755-4912-B40F-EDD5EC37CEBA")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class UDF
{
public string GetAppName()
{
return Globals.ThisAddIn.Application.ActiveSheet.Name; //Globals.ThisAddIn为null
}

[ComRegisterFunction]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
var key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
key.SetValue("", Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String);
}

[ComUnregisterFunction]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
}

private static string GetSubKeyName(Type type, string subKeyName)
{
var s = new System.Text.StringBuilder();
s.Append(@"CLSID\{");
s.Append(type.GUID.ToString().ToUpper());
s.Append(@"}\");
s.Append(subKeyName);
return s.ToString();
}

public void test()
{
object addInName = "ExcelImportData";
Microsoft.Office.Core.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName);
IAddInUtilities utilities = (IAddInUtilities)addIn.Object;
utilities.ImportData();
}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧