|
按照高人的例子做联系,在vs2010里面自定义函数可以生成,但是在excel调用的时候看不到,不清楚是什么问题,但是是因为 在弹出的窗口中点击编译页面,勾起“为 COM 互操作注册”,见下图 在弹出的窗口中点击编译页面,勾起“为 COM 互操作注册”,见下图
“在弹出的窗口中点击编译页面,勾起“为COM互操作注册”的原因吗?,代码如下
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Text
<ClassInterface(ClassInterfaceType.AutoDual), ComVisible(True)>
<Guid("207DF2C7-9773-4FB1-A50C-2A2515C3F8FA")>
Public Class Class1
Public Function MYFUNCTION(ByVal number1 As Double, ByVal number2 As Double) As Double
Return number1 * number2
End Function
<ComRegisterFunctionAttribute()> Public Shared Sub RegisterFunction(ByVal Type As Type)
Dim rk As RegistryKey
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(Type, "Programmable"))
rk = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(Type, "InprocServer32"), True)
rk.SetValue("", Environment.SystemDirectory + "\mscoree.dll", RegistryValueKind.String)
End Sub
<ComUnregisterFunctionAttribute()> Public Shared Sub UnregisterFunction(ByVal Type As Type)
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(Type, "Programmable"), False)
End Sub
Private Shared Function GetSubKeyName(ByVal Type As Type, ByVal SubKeyName As String) As String
Dim b As StringBuilder = New StringBuilder
b.Append("CLSID\{")
b.Append(Type.GUID.ToString().ToUpper())
b.Append("}\")
b.Append(SubKeyName)
GetSubKeyName = b.ToString()
End Function
End Class
|
|