Compile throws a “User-defined type not defined” error but does not go to the offending line of code

前端 未结 21 1302
再見小時候
再見小時候 2020-12-14 08:00

Symptoms

This is a symptom specifically when compiling an Excel VBA project. The following error occurs:

User-defined type not defin

21条回答
  •  死守一世寂寞
    2020-12-14 08:37

    My solution is not good news but at least it should work.

    My case is: I have a .xlsm from a coworker. 1) I open it and click the button: it works fine. 2) I save the file, close excel, open the file again: now it doesn't work anymore. The conclusion is: the code is OK but excel can't manage references correctly. (I've tried removing the re-adding the references without any success)

    So the solution is to declare every referenced object as Variant and use CreateObject("Foo.Bar") instead of New Foo.Bar.

    For example:

    Dim objXML As MSXML2.DOMDocument
    Set objXML = New MSXML2.DOMDocument
    

    Replaced by:

    Dim objXML As Variant
    Set objXML = CreateObject("MSXML2.DOMDocument")
    

提交回复
热议问题