我需要将符合以下条件的电子邮件保存在桌面文件夹中:
如果同时满足这两个条件,则会弹出“是/否”消息框。
代码:
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
问题:
所有主题行和所有用户的弹出窗口都会出现。
我尝试使用嵌套的If if,但是没有得到正确的输出。
整个代码在ThisOutlookSession中。
编辑1 ,
我删除了On Error Resume Next
。
修改后的代码是:
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
答案 0 :(得分:0)
建议的If / Else结构以及适当的邮件项目。
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