forked from Excel-DNA/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddIn.vb
More file actions
73 lines (58 loc) · 2.48 KB
/
Copy pathAddIn.vb
File metadata and controls
73 lines (58 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Imports ExcelDna.Integration
Imports ExcelDna.IntelliSense
Imports System.CodeDom.Compiler
Imports System.Reflection
Namespace TheAddin
Public Class MyAddIn
Implements IExcelAddIn
Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
IntelliSenseServer.Install()
' Get the default list of exported assemblies
Dim assemblies As New List(Of Assembly)
assemblies.AddRange(ExcelIntegration.GetExportedAssemblies())
' Compile and add the dyanmic assembly to the list
Dim dynamicAssembly As Assembly = CreateFunctionAssembly()
If Not dynamicAssembly Is Nothing Then
assemblies.Add(dynamicAssembly)
End If
' Use our own registration helper to do the registration
RegistrationHelper.PerformRegistration(assemblies)
End Sub
Public Sub AutoClose() Implements IExcelAddIn.AutoClose
IntelliSenseServer.Uninstall()
End Sub
Private Function CreateFunctionAssembly() As Assembly
Dim dq As String = """"
Dim c As VBCodeProvider = New VBCodeProvider
Dim cp As CompilerParameters = New CompilerParameters
Dim code As String = "Namespace NewFunctions
Public Module MyNewUDF
Function GETNAME()
Return " + dq + "My name is Jeff" + dq + "
End Function
Function GETAGE()
Return " + dq + "My age is 23" + dq + "
End Function
End Module
End Namespace"
Dim results As CompilerResults = c.CompileAssemblyFromSource(cp, code)
If results.Errors.HasErrors Then
For Each er As CompilerError In results.Errors
MsgBox(er.ErrorNumber + ". " + er.ErrorText)
Next
Return Nothing
End If
Return results.CompiledAssembly
End Function
End Class
End Namespace
Namespace OldFunctions
Public Module MyOldUDF
Function GETHEIGHT() As String
Return "Height is 170cm"
End Function
Function GETWEIGHT() As String
Return "Weight is 70KG"
End Function
End Module
End Namespace