Declaring early bound MSXML object throws an error in VBA

72,965

Solution 1

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

Solution 2

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.

Solution 3

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"

Solution 4

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

Share:
72,965
Admin
Author by

Admin

Updated on May 16, 2020

Comments

  • Admin
    Admin about 4 years

    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"

  • RossC
    RossC over 10 years
    Thank you! This was driving me mad. And the fancy pic seals the deal.
  • user734028
    user734028 about 10 years
    damn still using windows xp :|
  • Marcel Gosselin
    Marcel Gosselin over 9 years
    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/…
  • BlazingFrog
    BlazingFrog over 9 years
    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)
  • joehanna
    joehanna almost 9 years
    Nice find @MarcelGosselin. Also solves my problem on Windows Server 2012.
  • SierraOscar
    SierraOscar about 8 years
    wait - no freehand red circles? ;P
  • CarloC
    CarloC over 6 years
    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.
  • T.M.
    T.M. almost 5 years
    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.
  • MBB70
    MBB70 about 4 years
    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).