В VBA Outlook, как поставить условие на строку - строку темы? - PullRequest
0 голосов
/ 08 июля 2019

Я хочу автоматически сохранять вложения из подпапки в моем 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

1 Ответ

0 голосов
/ 08 июля 2019

Примерно так:

Поместите условие 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
...