Microsoft Word VBA 中的虚假“未定义用户定义类型”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1301103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:43:07  来源:igfitidea点击:

Spurious "User-defined type not defined" error in Microsoft Word VBA

vbams-wordword-vba

提问by gkrogers

I have a Microsoft Word template with some code and some references, that has been working fine for months but has just started throwing up a spurious "User-defined type not defined" error whenever I open it or try to compile it.

我有一个 Microsoft Word 模板,其中包含一些代码和一些参考资料,几个月以来一直运行良好,但每当我打开它或尝试编译它时,它就开始抛出一个虚假的“未定义用户定义的类型”错误。

I know it's a spurious error because I haven't made any significant changes to the code. In fact, I've rolled the code back to the last deployed version (which I know works fine) and I still get the error. I've also commented out all the code in the template and I still get the error. I've also removed and re-added all references (same error), and removed all the references and added them back, one by one, until the resultant compile errors are resolved, at which point I'm left with the spurious "User-defined type not defined" error. (I'm going to call this a UDTND error, from now, to avoid driving you all mad.) I think the error started popping up after I rebooted my PC. It only happens with this template, but I don't see how it can be anything to do with this template.

我知道这是一个虚假错误,因为我没有对代码进行任何重大更改。事实上,我已经将代码回滚到上次部署的版本(我知道它工作正常),但仍然出现错误。我还注释掉了模板中的所有代码,但仍然出现错误。我还删除并重新添加了所有引用(相同的错误),并删除了所有引用并将它们一个一个地添加回来,直到解决了由此产生的编译错误,此时我留下了虚假的“用户-定义的类型未定义”错误。(从现在开始,我将称其为 UDTND 错误,以免让你们发疯。)我认为在我重新启动 PC 后该错误开始出现。它只发生在这个模板上,但我看不出它与这个模板有什么关系。

Interestingly, the error is subtly different from a genuine UDTND error in the following ways:

有趣的是,该错误在以下方面与真正的 UDTND 错误略有不同:

  • No code is highlighted when the error is displayed.

  • The dialog is titled "Microsoft Visual Basic", and contains the error message but, unlike a real UDTND error, doesn't contain the text "Compile error:";

  • It happens when the template is opened, not just when it's compiled (at least, I think that's different from a normal error).

  • 显示错误时不会突出显示任何代码。

  • 该对话框标题为“Microsoft Visual Basic”,包含错误消息,但与真正的 UDTND 错误不同,它不包含文本“编译错误:”;

  • 它发生在模板打开时,而不仅仅是在编译时发生(至少,我认为这与正常错误不同)。

I've tried Googling it but I just get a bazillion results from novice developers asking why they get this error, with responses telling them that they either need to declare the missing type, correct the spelling of the offending variable type, or add a reference to a missing library. I've been banging my head against my screen all afternoon, and that's helped about as much as all the other things I've tried (i.e. not at all). I have a feeling that this is something to do with a messed-up reference, but afaict they're all fine, and I've removed and re-added them, which I would expect to resolve that sort of problem.

我试过在谷歌上搜索它,但我从新手开发人员那里得到了无数的结果,询问他们为什么会收到这个错误,回复告诉他们他们要么需要声明缺少的类型,更正有问题的变量类型的拼写,或者添加一个引用到一个失踪的图书馆。我整个下午都在用头撞屏幕,这和我尝试过的所有其他事情一样有用(即完全没有)。我有一种感觉,这与一个混乱的参考有关,但事实上它们都很好,我已经删除并重新添加了它们,我希望能解决这类问题。

Any ideas...?

有任何想法吗...?

回答by Smandoli

Your trouble-shooting on References is sound. Once upon a time (and I don't recall the precise error) I was at the same point, and the reference orderingwas the key. When you're designating References, you'll notice a "Priority" adjustment feature. Experiment with that and you may solve this.

您对 References 的故障排除是合理的。曾几何时(我不记得确切的错误)我在同一点上,参考顺序是关键。当您指定参考时,您会注意到“优先级”调整功能。试验一下,你可能会解决这个问题。

回答by 5Arete23

I have had "User-defined type not defined" problem on several occasions when compiling Microsoft Visual Basic 6 (MSVB6) code that was compiling without a problem earlier. It seems to happen after I have had a long coding session without rebooting the computer. As you can guess, I have been using Microsoft operating systems. I currently am using Windows XP. Rebooting the computer usually fixes the problem as it so often does on Microsoft operating systems.

在编译 Microsoft Visual Basic 6 (MSVB6) 代码时,我曾多次遇到“未定义用户定义的类型”问题,而该代码之前没有问题。它似乎发生在我没有重新启动计算机的长时间编码会话之后。您可以猜到,我一直在使用 Microsoft 操作系统。我目前使用的是 Windows XP。重新启动计算机通常可以解决问题,就像在 Microsoft 操作系统上经常发生的那样。

I have read that fully qualifying declarations also can help, e.g., "Dim oBar as Foo.Bar" instead of "Dim oBar as Bar". I have not tried this approach however.

我读过完全限定的声明也有帮助,例如,“Dim oBar as Foo.Bar”而不是“Dim oBar as Bar”。但是,我还没有尝试过这种方法。

回答by rdongart

I had a very similar problem.

我有一个非常相似的问题。

My problem appeared (I think) just after I made a Search and Replace that I canceled (Ctrl+Z). There was not highligting of the problem, only the ""User-defined type not defined" error message when I compiled.

我的问题出现(我认为)在我进行了我取消的搜索和替换(Ctrl + Z)之后。我编译时没有突出问题,只有“未定义用户定义的类型”错误消息。

I tried:

我试过:

1) restarting computer
2) changing reference ordering
3) removing functions/procedures, modules one at a time.

1) 重新启动计算机
2) 更改参考顺序
3) 一次删除一个功能/过程、模块。

Didn't work. My project was written in Excel VBA and here was the solution I found.

没用。我的项目是用 Excel VBA 编写的,这是我找到的解决方案。

THE SOLUTION:

解决方案:

I opened a new Excel file and opened the Visual Basic Editor. I then copied all Forms, Modules, and Class Modules one by one into the new file. I then Copied the Control Objects (3 Commandbuttons) from the old sheet into the new one. Now the new file was identical to the old project - only the ""User-defined type not defined" had gone and problem was solved.

我打开了一个新的 Excel 文件并打开了 Visual Basic 编辑器。然后我将所有表单、模块和类模块一一复制到新文件中。然后我将旧工作表中的控制对象(3 个命令按钮)复制到新工作表中。现在新文件与旧项目相同 - 只有“未定义的用户定义类型”消失了,问题解决了。

回答by Cool Blue

I had the same problem with Excel 2013.

我在 Excel 2013 中遇到了同样的问题。

It started when I did a search and replace on the name of a Custom Class. I changed the name of the Class after I did the search and replace on all references to it and the spurious error started immediately after that.

当我搜索并替换自定义类的名称时,它就开始了。我在搜索并替换了对它的所有引用后更改了类的名称,之后立即开始出现虚假错误。

I reverted to an earlier version ad confirmed that the problem was not there and then did the same search and replace and re-name and got the exact same behaviour again.

我恢复到早期版本的广告,确认问题不存在,然后进行相同的搜索、替换和重命名,并再次获得完全相同的行为。

The Custom Class that I changed the name of only had one consumer and it was also a custom class. I exported, removed and re-loaded the sole consumer class and the problem was fixed.

我改名的自定义类只有一个消费者,也是自定义类。我导出、删除并重新加载唯一的消费者类,问题得到解决。

回答by g_g

Yea references would be the first step in troubleshooting this problem as already stated, but failing that id start commenting out the code in any event procedures running at start up (my experience is only with Access VBA though)

是的,引用将是解决此问题的第一步,如前所述,但如果该 ID 失败,则开始在启动时运行的任何事件过程中注释掉代码(尽管我的经验仅适用于 Access VBA)

回答by Lance Roberts

Check out this linkfor a Microsoft bug that might be related.

查看此链接以了解可能相关的 Microsoft 错误。

TLDR:

域名注册地址:

The reference to a package/addin/whatever probably needs to be re-referenced. Check Tools -> Referencesin the Menu.

对包/插件/任何可能需要重新引用的引用。在菜单中检查工具 -> 参考

Also it appears that if you install Microsoft Security Advisory 960715, that certain controls are killed. There are fixes which may or may not work for you. A good article is on this blog:

此外,如果您安装 Microsoft 安全公告 960715,某些控件似乎也会被终止。有一些修复程序可能适合您,也可能不适合您。这个博客上有一篇很好的文章:

VSOD Blog

VSOD博客