Search code examples
xmlvbaxelement

Can I use XElement in VBA? (NOT in VB.NET)


A would like to use XElement in VBA in VISIO. (because I'd like port my c# routins which is used XElement etc.)

I can't found solution or I don't know how I use this.

VB.NET code like this but not working in VBA:

Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim employees As IEnumerable(Of XElement) = xelement.Elements()
' Read the entire XML
For Each employee In employees
      Console.WriteLine(employee)
Next employee

Solution

  • You can't use the .NET Framework DLLs in VBA. It would be possible for you to write your own .NET DLL that uses those Namespaces, make it COM visible, register it in Windows and add that as a Reference to your VBA project (Tools/References). It would be quite a bit of work - you'd have to decide whether it would be worth the effort...

    Depending on what you need, however, an alternate approach would be to use the MSXML parser. Add that as a Reference to your VBA project. It may seem a bit "old-fashioned" to someone who's used the .NET tools, but it should do the job of working with the DOM of an XML document. MSXML 6.0 is installed as part of the last few versions of Office.