问题
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"
回答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!
回答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.
回答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"
回答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
回答5:
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.
来源:https://stackoverflow.com/questions/11245733/declaring-early-bound-msxml-object-throws-an-error-in-vba