将Outlook Folder.items分配给Excel中的对象时,运行时错误为“ 5”

问题描述 投票:0回答:1

我遇到Excel报告的一些问题

运行时错误“ 5”

将Outlook Folder.items分配给对象时。

这是此主题的后续主题。 Outlook Selecting a Subfolder in the SharedMailbox using GetSharedDefaultFolder Automation error

解决自动化错误后,我遇到运行时错误5,运行代码时无效的过程调用或参数:'''设置olItem = MyFolder.Items'''

任何人都可以帮忙吗?!

Sub CountInboxSubjects()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim MyFolder As Outlook.MAPIFolder
Dim MyFolder1 As Outlook.MAPIFolder
Dim MyFolder2 As Outlook.MAPIFolder
Dim MyFolder3 As Outlook.MAPIFolder
Dim olMailItem As Outlook.MailItem
Dim propertyAccessor As Outlook.propertyAccessor
Dim olItem As Object
'Dim olItem As Outlook.Items
Dim dic As Dictionary
Dim i As Long
Dim Subject As String
Dim val1 As Variant
Dim val2 As Variant

val1 = ThisWorkbook.Worksheets("EPI_Data").Range("I2")
val2 = ThisWorkbook.Worksheets("EPI_Data").Range("I3")

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
'Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olShareName = olNs.CreateRecipient("[email protected]")
Set olFldr = olNs.GetSharedDefaultFolder(olShareName, olFolderInbox)

If ThisWorkbook.Worksheets("EPI_Data").Range("I5") = "Inbox" Then
    Set MyFolder = olFldr
    MsgBox (MyFolder)
ElseIf ThisWorkbook.Worksheets("EPI_Data").Range("I5") = "Feasibilities" Then
    Set MyFolder = olFldr.Folders("Feasibilities")
    MsgBox (MyFolder)
ElseIf ThisWorkbook.Worksheets("EPI_Data").Range("I5") = "FNC's" Then
    Set MyFolder = olFldr.Folders("Feasibilities").Folders("FNC's")
    MsgBox (MyFolder)
ElseIf ThisWorkbook.Worksheets("EPI_Data").Range("I5") = "ISAs - Actioned" Then
    Set MyFolder = olFldr.Folders("Feasibilities").Folders("ISAs - Actioned")
    MsgBox (MyFolder)
End If

Set olItem = MyFolder.Items
'Set myRestrictItems = olItem.Restrict("[ReceivedTime]>'" & Format$("01/01/2019 00:00AM", "General Date") & "' And [ReceivedTime]<'" & Format$("01/02/2019 00:00AM", "General Date") & "'")
Set myRestrictItems = MyFolder.Items.Restrict("[ReceivedTime]>'" & Format$(val1, "General Date") & "' And [ReceivedTime]<'" & Format$(val2, "General Date") & "'")

For Each olItem In myRestrictItems
        If olItem.Class = olMail Then
        Set propertyAccessor = olItem.propertyAccessor
        Subject = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E1D001E")
        If dic.Exists(Subject) Then dic(Subject) = dic(Subject) + 1 Else dic(Subject) = 1
    End If
Next olItem

With ActiveSheet
    .Columns("A:B").Clear
    .Range("A1:B1").Value = Array("Count", "Subject")
    For i = 0 To dic.Count - 1
        .Cells(i + 2, "A") = dic.Items()(i)
        .Cells(i + 2, "B") = dic.Keys()(i)
    Next
End With

结束子

excel vba outlook outlook-vba
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.