问题描述
我目前在用VSTO开发Excel的插件,里面有自定义菜单,也有些自定义公式。碰到个问题:在自定义公式里得不到当前的Application。Globals.ThisAddIn为null,不知道为什么;(
解决方案
解决方案二:
该回复于2011-12-27 12:00:13被版主删除
解决方案三:
usingSystem.Runtime.InteropServices;usingMicrosoft.Win32;usingSystem;usingSystem.Windows.Forms;usingMicrosoft.Office.Interop.Excel;namespaceUDFTest{[Guid("05DE8F39-D755-4912-B40F-EDD5EC37CEBA")][ClassInterface(ClassInterfaceType.AutoDual)][ComVisible(true)]publicclassUDF{publicstringGetAppName(){returnGlobals.ThisAddIn.Application.ActiveSheet.Name;//Globals.ThisAddIn为null}[ComRegisterFunction]publicstaticvoidRegisterFunction(Typetype){Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type,"Programmable"));varkey=Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type,"InprocServer32"),true);key.SetValue("",Environment.SystemDirectory+@"mscoree.dll",RegistryValueKind.String);}[ComUnregisterFunction]publicstaticvoidUnregisterFunction(Typetype){Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type,"Programmable"),false);}privatestaticstringGetSubKeyName(Typetype,stringsubKeyName){vars=newSystem.Text.StringBuilder();s.Append(@"CLSID{");s.Append(type.GUID.ToString().ToUpper());s.Append(@"}");s.Append(subKeyName);returns.ToString();}publicvoidtest(){objectaddInName="ExcelImportData";Microsoft.Office.Core.COMAddInaddIn=Globals.ThisAddIn.Application.COMAddIns.Item(refaddInName);IAddInUtilitiesutilities=(IAddInUtilities)addIn.Object;utilities.ImportData();}}
解决方案四:
usingMsExcel=Microsoft.Office.Interop.Excel;usingMicrosoft.Office.Core;publicvoidOnConnection(objectapplication,Extensibility.ext_ConnectModeconnectMode,objectaddInInst,refSystem.Arraycustom){applicationObject=application;addInInstance=addInInst;app=(MsExcel.Application)application;}MsExcel.Applicationexcelapp=(MsExcel.Application)app;
解决方案五:
usingMsExcel=Microsoft.Office.Interop.Excel;usingMicrosoft.Office.Core;publicvoidOnConnection(objectapplication,Extensibility.ext_ConnectModeconnectMode,objectaddInInst,refSystem.Arraycustom){applicationObject=application;addInInstance=addInInst;app=(MsExcel.Application)application;}MsExcel.Applicationexcelapp=(MsExcel.Application)app;
解决方案六:
sinitek123:你这个是共享的外接程序模式吧。