循环浏览所有Outlook收件箱,包括共享收件箱错误

时间:2018-08-15 20:57:15

标签: excel vba excel-vba outlook outlook-vba

我有代码可以搜索用户的Outlook并回复电子邮件,具体取决于您在工作表单元格中输入的主题词组。我确实在几天前就可以使用它,但是现在我似乎无法使其正常工作(已删除)。运行后,错误消息将持续显示代码行“ Set olitems = flrd.Items”,并显示“对象变量或未设置块变量”。我认为问题是End,如果我将其放置在任何地方,则代码不执行任何操作或显示相同的错误。

工作代码(工作时)的唯一其他问题是它的填充次数不止一次。我希望它只能填充一次。

Sub Display()

Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String
Dim allStores As Stores
Dim storeInbox As Folder
Dim j As Long

Set allStores = Session.Stores

For j = 1 To allStores.count
On Error Resume Next
Debug.Print i & " DisplayName - " & allStores(i).DisplayName
On Error GoTo 0

Set storeInbox = Nothing
On Error Resume Next
Set storeInbox = allStores(i).GetDefaultFolder(olFolderInbox)
On Error GoTo 0

If Not storeInbox Is Nothing Then
End if
     Set olItems = Fldr.Items

olItems.Sort "[Received]", True

For i = 1 To olItems.count
    signature = Environ("appdata") & "\Microsoft\Signatures\"

    If Dir(signature, vbDirectory) <> vbNullString Then
        signature = signature & Dir$(signature & "*.htm")
    Else
        signature = ""
    End If

    signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll

    Set olMail = olItems(i)

    If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
        If Not olMail.Categories = "Executed" Then
            Set olReply = olMail.ReplyAll

            With olReply
                .HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & _
                    "<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & _
                    Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & _
                    Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & _
                    "Regards," & "</p><br>" & signature & .HTMLBody
                .Display
                .Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & _
                    Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
            End With

            Exit For
            olMail.Categories = "Executed"

        End If
    End If

Next i
 Set Fldr = StoreInbox

Next

ExitRoutine:
Set allStores = Nothing
Set storeInbox = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

首先,摆脱On Error Resume Next行。 其次,从未设置Fldr变量。您是不是要使用storeInbox变量?

Sub Display()

Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String
Dim allStores As Stores
Dim storeInbox As Folder
Dim j As Long

Set allStores = Session.Stores

For j = 1 To allStores.count
On Error Resume Next
Debug.Print i & " DisplayName - " & allStores(i).DisplayName
On Error GoTo 0

Set storeInbox = Nothing
On Error Resume Next
Set storeInbox = allStores(i).GetDefaultFolder(olFolderInbox)
On Error GoTo 0

If Not storeInbox Is Nothing Then

     Set olItems = Fldr.Items

olItems.Sort "[Received]", True

For i = 1 To olItems.count
    signature = Environ("appdata") & "\Microsoft\Signatures\"

    If Dir(signature, vbDirectory) <> vbNullString Then
        signature = signature & Dir$(signature & "*.htm")
    Else
        signature = ""
    End If

    signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll

    Set olMail = olItems(i)

    If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
        If Not olMail.Categories = "Executed" Then
            Set olReply = olMail.ReplyAll

            With olReply
                .HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & _
                    "<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & _
                    Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & _
                    Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & _
                    "Regards," & "</p><br>" & signature & .HTMLBody
                .Display
                .Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & _
                    Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
            End With

            Exit For
            olMail.Categories = "Executed"

        End If
    End If

Next i

End if

Next

ExitRoutine:
Set allStores = Nothing
Set storeInbox = Nothing

End Sub