【问题标题】:How do I put a condition on the subject line?如何在主题行中添加条件?
【发布时间】:2019-07-08 03:40:57
【问题描述】:

我想自动保存 Outlook 子文件夹中的附件。

我只需要保存邮件中带有特定主题行 (inStr) 和收到时间的邮件,例如今天。

我有代码,但不知道如何添加条件、接收的主题和时间;我想在保存时重命名 Excel 附件。

Option Explicit
Const folderPath = "C:\Documents\nike\My Documents\emailTest\"

Sub CompanyChange()

    On Error Resume Next
    Dim ns As NameSpace
    Set ns = GetNamespace("MAPI")
    Dim Inbox As MAPIFolder
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)

    Dim searchFolder As String
    searchFolder = InputBox("What is your subfolder name?")

    Dim subFolder As MAPIFolder

    Dim Item As Object
    Dim Attach As Attachment
    Dim FileName As String
    Dim i As Integer

    If searchFolder <> "inbox" Then
        Set subFolder = Inbox.Folders(searchFolder)
        i = 0
        If subFolder.Items.Count = 0 Then
            MsgBox "There are no messages in the Inbox.", vbInformation, _
              "Nothing Found"
            Exit Sub
        End If
        For Each Item In subFolder.Items
            For Each Attach In Item.Attachments
                Attach.SaveAsFile (folderPath & Attach.FileName)   
                i = i + 1
            Next Attach
        Next Item

    Else
        i = 0
        If Inbox.Items.Count = 0 Then
            MsgBox "There are no messages in the Inbox.", _ 
              vbInformation, "Nothing Found"
            Exit Sub
        End If
        On Error Resume Next
        For Each Item In Inbox.Items
            For Each Attach In Item.Attachments
                FileName = folderPath & Attach.FileName
                Attach.SaveAsFile FileName
                i = i + 1
            Next Attach
        Next Item
    End If

End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    像这样:

    将 if 条件放在保存文件的循环之前。

       For Each Item In subFolder.Items
    
            If Item.Subject = "Subject you want to Macth with" Then  'Condition
    
              For Each Attach In Item.Attachments
    
                Attach.SaveAsFile (folderPath & Attach.FileName)
    
                 i = i + 1
              Next Attach
    
            End If
    
       Next Item
    

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 2021-08-23
      • 2019-08-15
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2014-06-08
      相关资源
      最近更新 更多