38

I am trying to import xml data into excel.. So the first line of the code is

Dim XMLDOC As MSXML2.DOMDocument

and this gives an error "user defined type not defined"

2
  • 2
    In addition to what Scott suggested, you could Dim ... As Object and avoid the early binding issue.
    – JimmyPena
    Jul 5, 2012 at 18:05
  • 1
    Please mark the Scott Holtzman's answer as the correct one.
    – Nicu Tofan
    Dec 3, 2013 at 23:11

5 Answers 5

51

Inside the VBE, Go to Tools -> References, then Select Microsoft XML, v6.0 (or whatever your latest is. This will give you access to the XML Object Library.

Updated with fancy pic!

enter image description here

6
  • 1
    Thank you! This was driving me mad. And the fancy pic seals the deal.
    – RossC
    Sep 2, 2013 at 8:05
  • damn still using windows xp :|
    – user734028
    Mar 5, 2014 at 20:33
  • 7
    As a side note, if you are on Windows 8, you may need to reference Microsoft XML v3 because v6 does not expose DOMDocument class. See thread social.technet.microsoft.com/Forums/en-US/… Oct 3, 2014 at 15:38
  • 8
    As a side note to the side note from @marcel-gosselin, you can still use v6 if you dim your variable as MSXML2.DOMDocument60 (note the 60 at the end) Nov 13, 2014 at 3:21
  • 3
    Nice find @MarcelGosselin. Also solves my problem on Windows Server 2012.
    – joehanna
    Jun 12, 2015 at 5:14
38

I had DOMDocument defined which needed Microsoft XML, v3.0 but I had Microsoft XML, v6.0 selected in references which caused the below error

"user defined type not defined".

The solution

The solution was to either change DOMDocument to DOMDocument60 (60 uses ver 6.0) or use the Microsoft XML, v3.0 reference with DomDocument.


Just a quick note, if anyone is using a different version, such as Microsoft XML, v4.0, then DOMDocument40 should be used. This is because the number at the end of the DOMDocument is specific to the version of the library being used.

2
  • 2
    I found that anytime users had to reboot excel, if DOMDocument is used in Windows10, the MS XML, v3.0 selected would go back to v6.0. The better route is to use DOMDocument60 IMHO.
    – CarloC
    Sep 8, 2017 at 17:28
  • Version 6.0 is the latest version, 3.0 the last stable version (loaded by default if no version number indicated); vers. 4.0 definitively cannot be recommended anymore in current applications.
    – T.M.
    Aug 7, 2019 at 18:07
4

I work with a VBA Excel Macro that someone else wrote and I was tasked with fixing it after recently upgrading from Windows 7 / Office 2010 to Windows 10 / Office 2016. I started to receive the same "user defined type not defined" compile error. My previous install also had MS XML v6.0 but apparently you have to specifically point to this version in your code on Windows 10 and/or Office 2016 (I wasn't able to confirm which upgrade caused the issue). I was able to resolve the issue by doing a Find/Replace on the following:

"DOMDocument" to "MSXML2.DOMDocument60"
"XMLHTTP" to "MSXML2.XMLHTTP60"
3

I am using Microsoft Windows 10 & Office 2016.

Using Microsoft XML 6.0 does not fix the problem.

Selecting Microsoft XML 3.0 fixed the compilation error

Microsoft XML 3.0 reference

1
  • I had a unique situation where my program correctly parsed the XML from one URL and failed when extracting from another (completely different) URL when using version 6. Reversing to version 3 made it work with both URLs. I changed all instances of DOMDocument60 to DOMDocument30. I closed and reopened Excel and did not have the issue described above where the reference reverts to version 6. (Using W10 and Office 2016).
    – MBB70
    Mar 13, 2020 at 14:11
0

I had the 3rd and 6th versions installed, and the project uses the 4th one. I installed the 4th version from https://www.microsoft.com/en-us/download/details.aspx?id=15697 and this solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.