Hi,
I've created simple UDF following the post:
http://blogs.msdn.com/cumgranosalis/archive/2006/08/03/ServerClientUDFsCompat1.aspx
and I've got my UDF working on SharePoint. I can open workbook in a web browser, change parameters, recalculate and it works fine.
Then I¡¯ve decided to add COM part and implement IDTExtensibility2 interface, following the post:
http://blogs.msdn.com/cumgranosalis/archive/2007/03/07/ServerClientUDFsCompat3.aspx
and when I open same workbook in a web browser and do recalculate workbook I am getting #NAME instead of the result.
I am assuming that I have proper settings on Share Point Server because same workbook and same SampleUDF.dll are working without code for IDTExtensibility2.
Here is the code, when COM part is commented out it works. When I rebuild project including code that was commented out I have problem described above:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Office.Excel.Server.Udf;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Win32;
using Extensibility;
namespace SampleUDF
{
[UdfClass]
[Guid(TestingUDFforF9.ClsId)]
[ProgId(TestingUDFforF9.ProgId)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class TestingUDFforF9 : Object//, IDTExtensibility2
{
public TestingUDFforF9() { }
const string ClsId = "F0652C22-EE75-4651-B958-D1C9EC1C693E";
const string ProgId = "SampleUDF.TestingUDFforF9";
//private Excel.Application m_app = null;
//private object addInInstance;
#region IDTExtensibility2 Members
/*
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
m_app = (Excel.Application)Application;
addInInstance = AddInInst;
}
public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
}
*/
#endregion
[UdfMethod(IsVolatile = true)]
public double MyDouble(double d)
{
// if (m_app != null)
// m_app.Volatile(Type.Missing);
return d * 9;
}
[UdfMethod(IsVolatile = true)]
public string CurrentTime()
{
//if (m_app != null)
// m_app.Volatile(Type.Missing);
return (DateTime.Now.ToLongTimeString());
}
[UdfMethod(IsVolatile = true)]
public string GetNthWord(string sentence, int index, string delimiter)
{
if (delimiter.Length > 1)
{
throw new InvalidOperationException();
}
string[] split = sentence.Split(delimiter[0]);
if (split.Length <= index)
{
throw new InvalidOperationException();
}
//if (m_app != null)
// m_app.Volatile(Type.Missing);
return split[index];
}
[ComRegisterFunction]
public static void RegistrationMethod(Type type)
{
if (typeof(TestingUDFforF9) != type)
return;
RegistryKey key = Registry.ClassesRoot.CreateSubKey
(@"CLSID\{" + ClsId + @"}\Programmable");
key.Close();
}
[ComUnregisterFunction]
public static void UnregistrationMethod(Type type)
{
if (typeof(TestingUDFforF9) != type)
return;
Registry.ClassesRoot.DeleteSubKey(@"CLSID\{" + ClsId + @"}\Programmable");
}
}
}
Thanks for your help.
Aleksandra