OutLook Macro If Else Statement Office 365

时间:2019-04-18 23:26:35

标签: sorting outlook outlook-vba

尝试使用我帮助创建的宏。我最初是在Office 2016中使用它的,但在Office 365中似乎无法使用。如何使它再次工作?

    Option Explicit
    Public WithEvents objMails As Outlook.Items
    Private Sub Application_Startup()
        Set objMails = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
    End Sub

    Private Sub objMails_ItemAdd(ByVal Item As Object)
        Dim objMail As Outlook.MailItem
        Dim objAttachments As Outlook.Attachments
        Dim objAttachment As Outlook.Attachment
        Dim strAttachmentName As String
        Dim objInboxFolder As Outlook.Folder
        Dim objTargetFolder As Outlook.Folder

        Debug.Print "Items Add"

        '"Ensure the incoming item is an email"
        If TypeOf Item Is MailItem Then
            Set objMail = Item
            Set objAttachments = objMail.Attachments

            '   "Check if the incoming email contains one or more attachments"
            If objAttachments.Count > 0 Then
                For Each objAttachment In objAttachments
                    strAttachmentName = objAttachment.DisplayName
                    Debug.Print strAttachmentName

                    Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)

                    If InStr(LCase(strAttachmentName), "attachment name") > 0 Then
                        Set objTargetFolder = objInboxFolder.Folders("Target Folder")
                        objMail.Move objTargetFolder
                        Debug.Print objAttachment.DisplayName
                    End If
                Next
            End If
        End If

        Set objMail = Nothing
        Set objAttachments = Nothing
        Set objAttachment = Nothing
        Set objInboxFolder = Nothing
        Set objTargetFolder = Nothing
    End Sub

运行宏时,我看不到任何错误。我确实确保附件名称全部为小写。启用了宏执行。文件夹名称正确。

0 个答案:

没有答案