我试图将MailItem对象存储在我的自定义对象的属性中以便稍后调用,但我收到以下错误:
Run-time error '91': Object variable or With block variable not set
这是我的Class1:
Private vprop1 As String
Private vprop2 As String
Private vprop3 As MailItem
Property Get prop1() As String
prop1 = vprop1
End Property
Property Let prop1(aValue As String)
vprop1 = aValue
End Property
Property Get prop2() As String
prop2 = vprop2
End Property
Property Let prop2(aValue As String)
vprop2 = aValue
End Property
Property Get prop3() As MailItem
prop3 = vprop3
End Property
Property Let prop3(aValue As MailItem)
vprop3 = aValue
End Property
这是我的考试。该错误引用了第三个属性赋值:var.prop3 = ...
Public Sub test()
Set objsourcefolder = Application.Session.GetDefaultFolder(olFolderInbox)
Dim var As Class1
Set var = New Class1
var.prop1 = "\folder\sub\12345"
var.prop2 = "Email subject regarding 12345"
var.prop3 = objsourcefolder.Items.Item(objsourcefolder.Items.count)
Debug.Print var.prop1
Debug.Print var.prop2
Debug.Print var.prop3
Debug.Print var.prop3.Subject
End Sub
非常感谢任何帮助!
答案 0 :(得分:1)
由于MailItem
是object
,您必须设置通过Set
关键字引用它的任何变量并更改Property Let
(与not object
相关联1}}类型属性)到Property Set
(与object
类型属性相关联)
最后,您无法使用Debug.Print var.prop3
,因为Class1
类型的prop3
object
成员没有默认属性
以上所有结果:
班级代码
Private vprop1 As String '<--| vprop1 is declared as of 'String' type, i.e. NOT as an 'object'
Private vprop2 As String '<--| vprop2 is declared as of 'String' type, i.e. NOT as an 'object'
Private vprop3 As MailItem '<--| vprop3 is declared as of 'MailItem' type, i.e. as an 'object'
Property Get prop1() As String
prop1 = vprop1
End Property
Property Let prop1(aValue As String)
vprop1 = aValue
End Property
Property Get prop2() As String
prop2 = vprop2
End Property
Property Let prop2(aValue As String)
vprop2 = aValue
End Property
Property Get prop3() As MailItem
Set prop3 = vprop3 '<--| use 'Se't keyword
End Property
Property Set prop3(aValue As MailItem) '<--| use 'Property Set' instead of 'Property Let'
Set vprop3 = aValue '<--| use 'Set' keyword
End Property
测试子代码
Public Sub test()
Dim objsourcefolder As Folder
Set objsourcefolder = Application.Session.GetDefaultFolder(olFolderInbox)
Dim var As Class1
Set var = New Class1
var.prop1 = "\folder\sub\12345"
var.prop2 = "Email subject regarding 12345"
Set var.prop3 = objsourcefolder.Items.Item(objsourcefolder.Items.Count)
Debug.Print var.prop1
Debug.Print var.prop2
' Debug.Print var.prop3 '<--| this would result in an 438 error
Debug.Print var.prop3.Subject
End Sub