# How to use VBA types with UNDERLINE in it's name in NETLink?

When coding with VBA functions in NETLink, I need the enum reference. It's ok to get the NETTypInfo with code:

LoadNETType[
"Microsoft.Vbe.Interop.vbext_ComponentType"] // NETTypeInfo


but when to get the get the enum value, it fails

NETObjectToExpression[vbext_ComponentTypevbext _ct _StdModule]


because of conflicts in vbext _ct _StdModule coming up with the syntax UNDERLINE pattern, how to write the correct code in this case? Although I can get the enum value from other tools , I want use the enum names visually for reading.

• On my machine the following code Needs["NETLink"];LoadNETType[ "Microsoft.Vbe.Interop.vbext_ComponentType"] // NETTypeInfo doesn't work. ShouId I add some code (for example load a Assembly) ? – andre314 Nov 24 '18 at 11:10
• @andre Please check up whether there be Microsoft.Vbe.Interop.dll registered at C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop. refering to docs.microsoft.com/en-us/visualstudio/vsto/…. – Jerry Nov 24 '18 at 11:48
• As much fun as I had answering this question, I am voting to close on the grounds that the answer is easy to find in the documentation. – WReach Nov 24 '18 at 17:20
• @WReach Thanks! I didnot read through the manual, it's solved. – Jerry Nov 24 '18 at 17:52
• But, as LoadNETAssembly[]  can use either the name of the file that contains the assembly or the name of the assembly itself (when the assembly is in the GAC, which is the case here), I have found the name of the assembly by trial and error (turns out to be "Microsoft.Vbe.Interop", but note that this name may be totally different from the name of the file). So after having evaluated LoadNETAssembly["Microsoft.Vbe.Interop"], I was able to reproduce OP's problem. – andre314 Nov 24 '18 at 23:13

Quoting from the Underscores in .NET Names subsection within the Calling .NET from the Wolfram Language section of the NETLink Tutorial:

.NET/Link maps underscores in type, method, property, and field names to "U".

So me must use:

vbextUComponentTypevbextUctUStdModule


The documentation does not mention it, but in the extremely unlikely event that this mangled name conflicts with another symbol, the one which is defined first takes precedence and the other is unavailable.

Self-contained Example

The question uses an example from the Microsoft Office interop library, which readers may not have. We can use the standard library class System._AppDomain to see the effect:

InstallNET[];

UAppDomainReferenceEquals[Null, Null]
(* True *)


Just For Fun

A closer simulation of the original problem is hard to come by using just the standard library since underscores are discouraged .NET standard naming conventions. Just for fun, we can dynamically generate our own DLL that is closer to the example in the question:

First, we generate an assembly that has symbol names containing underscores:

Needs["NETLink"]
InstallNET[];

$$myDll = FileNameJoin @ {$$TemporaryDirectory, "MyNs.dll"};

NETBlock @ Module[{provider, parameters, result}
, provider = NETNew["Microsoft.CSharp.CSharpCodeProvider"]
; parameters = NETNew["System.CodeDom.Compiler.CompilerParameters"]
; parameters@OutputAssembly = $myDll ; provider@CompileAssemblyFromSource[parameters, "namespace MyNs { public class vbext_ComponentType { public const string vbext_ct_StdModule = \"fake module\"; } }" // List ] ];  With that ugliness aside, we can now load and access the problematic symbols: LoadNETAssembly[$myDll];

vbextUComponentTypevbextUctUStdModule
(* "fake module" *)


Clean up:

UninstallNET[];
DeleteFile[\$myDll]

• Although _ could be substitued by U in codes, but still problem comes up in the value given to Property, i.e. @NumberFormatLocal = "0.00_" in which `_' should be removed or exceptions occur. – Jerry Nov 25 '18 at 10:08