VBA 中未定义文档类型

Document type not defined in VBA

我正在关注这个 mail merge work that I found in a related Stack Exchange question,它看起来与我想要的类似,但我无法开始。我已将问题缩小到以下范围...

Sub TestEmailer()
    Dim Source As Document, Maillist As Document, TempDoc As Document
End Sub

有错误"User-defined type not defined."

我似乎缺少一个参考,但推荐的参考 "Microsoft Office 16.0 Object Library" 在我启用它时似乎没有解决问题。

此文档对象是否已移至其他库?以后如何找到合适的图书馆?

如果您在 Excel 中执行此操作,则需要添加对 Microsoft Word 16.0 Object Library 的引用,然后使用:

Sub TestEmailer()
    Dim Source As Word.Document, Maillist As Word.Document, TempDoc As Word.Document
End Sub

默认情况下,每个应用程序将只加载它自己的对象,以及其他常见的 Office 对象。如果您想使用来自另一个应用程序的其他对象,您需要添加对该应用程序对象库的引用或使用后期绑定。

在这种情况下,我建议提前绑定(手动添加引用),以便您在代码中获得 IntelliSense 的好处,这将使跨应用程序工作变得更容易。