对象不支持自定义类的此属性或方法错误

时间:2012-12-29 16:12:43

标签: vba outlook-vba

我仍然对此问题感到困惑。我正在使用自定义类模块:TimeFrame和TimeSheet。以下是缩短版本:

_________________________
'TIMEFRAME CLASS MODULE
'This class records Military Time, but allows
'you to also display it in Civilian Time
Private pHours As Integer
Private pMinutes As Integer
Private pTotalMinutes As Integer

Public Property Get hours() As Integer
  hours = pHours
End Property
Public Property Let hours(Value As Integer)
  If Value >= 1 And Value <= 24 Then
     pHours = Value
  Else
     Err.Raise 9009, , "Hours Value [" & CStr(Value) & "] is not valid."
  End If
End Property
Public Property Get minutes() As Integer
  minutes = pMinutes
End Property
Public Property Let minutes(Value As Integer)
  If Value >= 0 And Value <= 59 Then
    pMinutes = Value
  Else
    Err.Raise 9010, , "Minutes Value [" & CStr(Value) & "] is not valid."
  End If
End Property

Public Property Let Initialize(str As String)
  'This property expects a string formatted like: 08:30
  Dim vhours As String
  Dim vminutes As String
  Dim arrTime() As String

  If InStr(str, Me.Delimiter) Then
    arrTime = Split(str, Me.Delimiter)
    vhours = Trim(arrTime(0))
    vminutes = Trim(arrTime(1))
    If IsNumeric(vhours) Then
       pHours = CInt(vhours)
    End If
    If IsNumeric(vminutes) Then
       pMinutes = CInt(vminutes)
    End If
  Else
    'Set to defaults
    pHours = 0
    pMinutes = 0
  End If
End Property
...

_____________________________
'TIMESHEET CLASS MODULE
Private pEmployeeID As Integer
Private pStartDate As Date
Private pEndDate As Date
Private pMonStart As TimeFrame
Private pMonEnd As TimeFrame
Private pMonBreak As Double
Private pTuesStart As TimeFrame
Private pTuesEnd As TimeFrame
Private pTuesBreak As Double
Private pWedStart As TimeFrame
Private pWedEnd As TimeFrame
Private pWedBreak As Double
Private pThursStart As TimeFrame
Private pThursEnd As TimeFrame
Private pThursBreak As Double
Private pFriStart As TimeFrame
Private pFriEnd As TimeFrame
Private pFriBreak As Double
Public Property Get EmployeeID() As Integer
    EmployeeID = pEmployeeID
End Property
Public Property Let EmployeeID(Value As Integer)
    If Value > 0 Then
           pEmployeeID = Value
    Else
         MsgBox "Employee ID " & Value & " is an incorrect value." & vbCrLf & "Employee ID must be a positive integer"
    End If

End Property
Public Property Get StartDate() As Date
  StartDate = pStartDate
End Property
Public Property Let StartDate(Value As Date)
  pStartDate = Value
End Property
Public Property Get EndDate() As Date
  EndDate = pEndDate
End Property
Public Property Let EndDate(Value As Date)
  pEndDate = Value
End Property
Public Property Get MondayStart() As TimeFrame
  Set MondayStart = pMonStart
End Property
Public Property Set MondayStart(ByRef Value As TimeFrame)
  Set pMonStart = Value
End Property
Public Property Get MondayEnd() As TimeFrame
  Set MondayEnd = pMonStart
End Property
Public Property Set MondayEnd(ByRef Value As TimeFrame)
  Set pMonEnd = Value
End Property
Public Property Get MondayBreak() As Double
  MondayBreak = pMonBreak
End Property
Public Property Let MondayBreak(Value As Double)
  pMonBreak = Value
End Property
Public Property Get TuesdayStart() As TimeFrame
  Set TuesdayStart = pTuesStart
End Property
Public Property Set TuesdayStart(ByRef Value As TimeFrame)
  Set pTuesStart = Value
End Property
Public Property Get TuesdayEnd() As TimeFrame
   Set TuesdayEnd = pTuesStart
End Property
Public Property Set TuesdayEnd(ByRef Value As TimeFrame)
   Set pTuesEnd = Value
End Property
Public Property Get TuesdayBreak() As Double
  TuesdayBreak = pTuesBreak
End Property
Public Property Let TuesdayBreak(Value As Double)
  pTuesBreak = Value
End Property
...

在我的主模块中,我一个接一个地调用两个子程序。

  1. 我有一个名为TimeSheetCollection的全局变量,它是一个包含TimeSheet类型实例的集合
  2. 首先,我调用子程序ReadWeeklyTimeSheets(),它设置TimeSheetCollection对象,读取今天的邮件项目,并从我们在每个TimeSheet中设置属性的mailitem主体获取的值。这个子程序运行得很好。
  3. 接下来,调用名为ExportTimeSheetsToDatabase()的子例程。我输入了几个Debug.print语句,我在Item.MondayStart和Item.MondayEnd属性上收到错误消息:“对象不支持此属性或方法”
  4. 主要模块:

    Sub ExportTimeSheetsToDatabase()
      Dim Item As TimeSheet
      For Each Item In TimeSheetCollection
       Debug.Print "TSCollection Count: " & TimeSheetCollection.count
       Debug.Print Item.EmployeeID & ", " & Item.StartDate & ", " & Item.EndDate
       Debug.Print Item.MondayStart  '<<< Error occurs here
       Debug.Print Item.MondayEnd
       Debug.Print "Toal Hours: " & Item.TotalWeeklyHours
      Next Item
    End Sub
    

    由于Item的类型为TimeSheet,您可以从上面的类模块中看到它确实包含Get方法:

    Public Property Get MondayStart() As TimeFrame
      Set MondayStart = pMonStart
    End Property
    

    从我的研究中,语法看起来是正确的,当我输入点运算符“。”在项目列表显示后显示所有属性。这不像是我拼错了什么。请指教。

    艾伦

1 个答案:

答案 0 :(得分:2)

Debug.Print不适用于对象。您需要在返回标准数据类型的属性或方法上使用它。否则,您将始终收到错误“对象不支持此属性或方法”。

例如,您需要TimeFrame.ValueTimeFrame.MilitaryValue

之类的内容

可能的实施:

Public Property Get Value() As String
    'Returns TimeFrame TimeStamp as standard time
     Value = CDate(pHours & ":" & pMinutes)
End Property

Public Property Get MilitaryValue() as String
     'Returns TimeFrame TimeStamp as Military Time
     MilitaryValue = Format(CDate(pHours & ":" & pMinutes),"hh:nn")
End Property

根据您对TimeFrame的实现,它似乎是一个时间戳和持续时间。在这种情况下,使用标准Date数据类型可能更有意义。日期数据类型精确到秒,您可以使用小时和分钟功能提取特定的小时和分钟信息。例如: Hour(timeStamp)Minute(timeStamp)

然后,您可以使用简单的debug.print显示时间,默认为标准时间,但可以使用Format显示军事时间,如MilitaryValue示例所示。