[英]Late binding compile error: User-defined type not defined referencing Outlook mailitem in Excel VBA
I am using late binding from Excel. 我正在使用Excel的后期绑定。
I get 我懂了
Compiler Error: User defined type not defined
编译器错误:未定义用户定义的类型
for: 对于:
If TypeOf olMail Is MailItem Then
I declare the following variables: 我声明以下变量:
Dim olApp As Object
Dim olNs As Object
Dim Fldr As Object
Dim olItms As Object
Dim olMail As Object
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = Fldr.Items
The code was working but I had to change to late binding because a user is not using the same Outlook version. 该代码正常工作,但由于用户使用的Outlook版本不同,我不得不更改为后期绑定。
I'm guessing the reason you get the error is because you no longer have a reference to the outlook object library, and thus MailItem
is an unknown type. 我猜您收到错误的原因是因为您不再有对Outlook对象库的引用,因此
MailItem
是未知类型。
Instead of using TypeOf
而不是使用
TypeOf
If TypeOf olMail Is MailItem Then
use the TypeName
function to compare the object type name to a string... 使用
TypeName
函数将对象类型名称与字符串进行比较...
If TypeName(olMail) = "MailItem" Then
(note I'm assuming the type name will be MailItem
, but you can always throw in MsgBox TypeName(olMail)
just to be sure!) (请注意,我假设类型名称将为
MailItem
,但是为了确保可以随时输入MsgBox TypeName(olMail)
!)
Type Name function is somewhat expensive. 类型名称功能有些昂贵。
Class
property (implemented by all OOM objects) would be a better aalternative. Class
属性(由所有OOM对象实现)将是更好的替代方法。 Check that oMail.Class = 43
(43 is OlObjectClass.olMail
constant) 检查
oMail.Class = 43
(43是OlObjectClass.olMail
常数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.