我每天早上运行一个任务,以便在Excel文件中运行宏。我的任务没有工作,因为VBA代码现在给了我一个错误。在今天之前,VBA代码是100%的功能。
我得到了
“对象不支持此属性或方法”
Dim olApp As Object
Dim olNS As Object
Dim myDate As Date
Dim olItems As Object
Dim olItem As Object
Dim olAttach As Object
Dim Date1 As String
Dim Date2 As String
Dim iAttachments As Integer
Date1 = Date & " " & TimeValue("6:00:00")
Date2 = Date & " " & TimeValue("00:00:00")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
Err.Clear: On Error GoTo 0
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items
For Each olItem In olItems
If olItem.ReceivedTime < Date1 Then '<----- ERROR LINE
If olItem.ReceivedTime > Date2 Then
If InStr(olItem.Body, "Darth Vader") > 0 Then
iAttachments = olItem.Attachments.Count + iAttachments
Set olAttach = olItem.Attachments.Item(1)
On Error GoTo Err_Handler
olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.Filename
Set olAttach = Nothing
Set olItem = Nothing
If iAttachments = 4 Then Exit For
End If
End If
End If
Next
Set olAttach = Nothing
Set olItem = Nothing
Set olApp = Nothing
Set olNS = Nothing
Set olItems = Nothing
Exit Sub
发布于 2019-05-09 12:56:40
所以我能解决我自己的问题。我不知道为什么我的代码在今天之前是100%工作的,但我确实做了一个调整,以便在Excel日期和Outlook日期之间有一个更兼容的语法。下面是修改后的代码,它将我的Excel日期格式更改为与Outlook日期格式相匹配。另外,我决定在我的时间范围内限制我的olItems,然后循环我的条件,而不是"IF“条件。
Dim olApp As Object
Dim olNS As Object
Dim myDate As Date
Dim olItems As Object
Dim olItem As Object
Dim olAttach As Object
Dim Date1 As String
Dim Date2 As String
Dim iAttachments As Integer
Date1 = Date & " " & TimeValue("6:00:00 am")
Date11 = Format(Date1, "ddddd h:nn AMPM") <----- Date to match Outlook format
Date2 = Date & " " & TimeValue("00:00:00 am")
Date22 = Format(Date2, "ddddd h:nn AMPM") <----- Date to match Outlook format
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
Err.Clear: On Error GoTo 0
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items.Restrict("[ReceivedTime] > """ & Date22 & """ and [ReceivedTime] < """ & Date11 & """") <----- Restricted my olItems to my specific range
For Each olItem In olItems
If InStr(olItem.Body, "Darth Vader") > 0 Then
iAttachments = olItem.Attachments.Count + iAttachments
Set olAttach = olItem.Attachments.Item(1)
On Error GoTo Err_Handler
olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.Filename
Set olAttach = Nothing
Set olItem = Nothing
If iAttachments = 4 Then Exit For
End If
Next
Set olAttach = Nothing
Set olItem = Nothing
Set olApp = Nothing
Set olNS = Nothing
Set olItems = Nothing
Exit Sub
发布于 2019-05-09 10:50:50
MailItems
或其他方式可能没有ReceivedTime
属性。由于您只关心MailItem
类型,所以您应该能够在For Each
中使用以下条件检查
For Each olItem In olItems
'With early binding, you could use:
' If TypeOf olItem Is MailItem Then
'Otherwise:
If TypeName(olItem) = "MailItem" Then
If olItem.ReceivedTime < Date1 Then ' <----- ERROR LINE
If olItem.ReceivedTime > Date2 Then
If InStr(olItem.Body, "Darth Vader") > 0 Then
iAttachments = olItem.Attachments.Count + iAttachments
Set olAttach = olItem.Attachments.Item(1)
On Error GoTo Err_Handler
olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.Filename
Set olAttach = Nothing
Set olItem = Nothing
If iAttachments = 4 Then Exit For
End If
End If
End If
Next
https://stackoverflow.com/questions/56065437
复制