+ -
当前位置:首页 → 问答吧 → VSTO开发Excel自定义公式的问题

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

时间:2011-12-27

来源:互联网

我目前在用VSTO开发Excel的插件,里面有自定义菜单,也有些自定义公式。碰到个问题:在自定义公式里得不到当前的Application。Globals.ThisAddIn为null,不知道为什么 ;(

作者: zqsntws   发布时间: 2011-12-27

该回复于2011-12-27 12:00:13被管理员删除

  • 对我有用[0]
  • 丢个板砖[0]
  • 引用
  • 举报
  • 管理
  • TOP
#2楼 得分:0回复于:2011-12-27 13:15:59
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();
  }
}

作者: zqsntws   发布时间: 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