Wie Outlook-E-Mails auf der Basis von Subjekt und Absendernamen, mit VBA zu retten?

stimmen
0

Ich brauche in einem Desktop-Ordner, E-Mails zu speichern, die die folgenden Bedingungen erfüllen:

  1. Betreff beginnt mit RE: FÜR REVIEW
  2. Absendernamen sind: Alpha, Beta oder Gamma (Beispiel)

Wenn diese beiden Bedingungen erfüllt sind, sollte ein Ja / Nein MsgBox Pop-up.

Code:

Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
 Dim FSO
 Dim xMailItem As Outlook.MailItem
 Dim xFilePath As String
 Dim xRegEx
 Dim xFileName As String
 Dim Output As String
 Dim Item As Object
 On Error Resume Next

  If (Item.Subject Like RE:FOR REVIEW*) And ((Item.SenderName = Alpha) Or (Item.SenderName = Beta) or (Item.SenderName = Gamma) ) Then
   Output = MsgBox(Do you want to save this email?, vbYesNo + vbQuestion, Reminder)
   If Output = vbNo Then Exit Sub
    Else
     xFilePath = CreateObject(WScript.Shell).SpecialFolders(16)
     xFilePath = C:\Users\ABC\Desktop\Test
     Set FSO = CreateObject(Scripting.FileSystemObject)
     If FSO.FolderExists(xFilePath) = False Then
      FSO.CreateFolder (xFilePath)
     End If
     Set xRegEx = CreateObject(vbscript.regexp)
    xRegEx.Global = True
    xRegEx.IgnoreCase = False
    xRegEx.Pattern = \||\/|\<|\>||:|\*|\\|\?
    If objItem.Class = olMail Then
     Set xMailItem = objItem
    xFileName = xRegEx.Replace(xMailItem.Subject, )
    xMailItem.SaveAs xFilePath & \ & xFileName & .html, olHTML
    End If
   End If

Exit Sub
End Sub

Problem:
Das Pop - up kommt für alle die Betreffzeile und all Nutzer.

Ich habe versucht, verschachtelte Verwendung Wenn sonst aber nicht bekam die richtige Ausgabe.

Der gesamte Code ist in Dieseoutlooksitzung.

Edit 1 , entfernte ich die On Error Resume Next.

Der bearbeitete Code ist:

Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
 Dim FSO
 Dim xMailItem As Outlook.MailItem
 Dim xFilePath As String
 Dim xRegEx
 Dim xFileName As String
 Dim Output As String

  If objItem.Class = olMail Then '**
  Set xMailItem = Application.CreateItem(olMailItem) '**

  If (xMailItem.Subject Like RE:FOR REVIEW*) And ((xMailItem.SenderName = Alpha) Or (xMailItem.SenderName = Beta) or (xMailItem.SenderName = Gamma) ) Then
     Output = MsgBox(Do you want to save this email?, vbYesNo + vbQuestion, Reminder)
    If Output = vbNo Then Exit Sub
     Else
      xFilePath = CreateObject(WScript.Shell).SpecialFolders(16)
      xFilePath = C:\Users\abc\Desktop\Test
      Set FSO = CreateObject(Scripting.FileSystemObject)
      If FSO.FolderExists(xFilePath) = False Then
       FSO.CreateFolder (xFilePath)
      End If
      Set xRegEx = CreateObject(vbscript.regexp)
     xRegEx.Global = True
     xRegEx.IgnoreCase = False
     xRegEx.Pattern = \||\/|\<|\>||:|\*|\\|\?
     If objItem.Class = olMail Then
      Set xMailItem = objItem
     xFileName = xRegEx.Replace(xMailItem.Subject, )
     xMailItem.SaveAs xFilePath & \ & xFileName & .html, olHTML
     End If
    End If
  End If
Exit Sub
End Sub
Veröffentlicht am 27/11/2018 um 17:48
quelle vom benutzer
In anderen Sprachen...                            


1 antworten

stimmen
0

Eine If / Else-Struktur mit entsprechendem MailItem vorgeschlagen.

Option Explicit

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)

    Dim FSO
    Dim xMailItem As MailItem
    Dim xFilePath As String
    Dim xRegEx
    Dim xFileName As String

    If objItem.Class = olMail Then

        'objItem could be used directly but this is sometimes beneficial
        Set xMailItem = objItem

        If (xMailItem.subject Like "RE:FOR REVIEW*") Then

            If ((xMailItem.senderName = "Alpha") Or _
                (xMailItem.senderName = "Beta") Or _
                (xMailItem.senderName = "Gamma")) Then

                If MsgBox("Do you want to save this email?", vbYesNo + vbQuestion, "Reminder") = vbYes Then

                    xFilePath = "C:\Users\abc\Desktop\Test"

                    Set FSO = CreateObject("Scripting.FileSystemObject")
                    If FSO.FolderExists(xFilePath) = False Then
                        FSO.CreateFolder (xFilePath)
                    End If

                    Set xRegEx = CreateObject("vbscript.regexp")
                    xRegEx.Global = True
                    xRegEx.IgnoreCase = False
                    xRegEx.Pattern = "\||\/|\<|\>|""|:|\*|\\|\?"

                    xFileName = xRegEx.Replace(xMailItem.subject, "")

                    xMailItem.SaveAs xFilePath & "\" & xFileName & ".html", olHTML

                End If

            End If

        End If

    End If

End Sub
Beantwortet am 28/11/2018 um 17:20
quelle vom benutzer

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more