1

我仍然很难解决这个问题。我正在使用自定义类模块: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 中的属性。这个子程序工作得很好。
  3. 接下来,调用名为 ExportTimeSheetsToDatabase() 的子例程。我输入了几个 Debug.print 语句,在 Item.MondayStart 和 Item.MondayEnd 属性上收到错误消息:“对象不支持此属性或方法”

主要模块:

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 类型,您可以从上面的 Class Module 中看到它确实包含 Get 方法:

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

根据我的研究,语法似乎是正确的,当我键入点运算符“。”时 在 Item 我的列表出现后,显示所有属性。这不像我拼错了什么。请指教。

艾伦

4

1 回答 1

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 和 Minute 函数提取特定的小时和分钟信息。例如: Hour(timeStamp)Minute(timeStamp)

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

于 2012-12-29T18:26:49.043 回答