首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Outlook Macro If Outlook语句

Outlook Macro If Outlook语句
EN

Stack Overflow用户
提问于 2018-04-11 00:03:23
回答 1查看 744关注 0票数 1

我的代码根据附件名称对电子邮件进行排序。我需要别人的帮助。

我希望不符合参数的电子邮件移动到主收件箱。

现在,任何不满足参数的东西只会移动到另一个文件夹。

正确的语法是什么?

代码语言:javascript
复制
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

"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
          Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)

          "Check the names of all the attachments"
          "Specify the target folders"

          If InStr(LCase(strAttachmentName), "some attachment name") > 0 Then
             Set objTargetFolder = objInboxFolder.Folders("Target Folder")
             Else: Set objTargetFolder = objInboxFolder.Folders("Target Folder 2")
     End If
     Next
     Move the email to specific folder
      objMail.Move objTargetFolder
   End If
End If

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

End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-11 01:01:21

您不需要设置收件箱,项目已经在收件箱中-您所做的一切是检查新添加的项目收件箱有附件名称,然后移动它

所以您的if语句应该如下所示

代码语言:javascript
复制
    'Check if the incoming email contains one or more attachments"
    If objAttachments.Count > 0 Then
        For Each objAttachment In objAttachments
           strAttachmentName = objAttachment.DisplayName

           Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)

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

完整代码应该如下所示

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49764482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档