PDA

View Full Version : Save outlook mail multiple locations depending on subject



xDanez
05-06-2019, 06:41 AM
Hello,

I'm currently trying to do a VBA script that will save a file to a different location based on the subject line.
My vba experience is limited to none but currently the script runs and upon receiving an email it saves the mail to BOTH folders no matter the subject line.

It's quite important that it only saves to one of the folders as I have a bot that runs depending on where a file is saved

Hope someone more qualified has an idea

Thanks in advance



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


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 sn As String

Dim oItem As MailItem
On Error Resume Next
xFilePath = xFilePath & "C:\New Folder\"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(xFilePath) = False Then
fso.CreateFolder (xFilePath)
End If
xFilePath1 = xFilePath1 & "C:\Outlook\"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(xFilePath1) = False Then
fso.CreateFolder (xFilePath1)
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, "")








If InStr(itm.Subject, "Yes") > 0 Then
xMailItem.SaveAs xFilePath1 & "\" & xFileName & ".html", olHTML
End If
If InStr(itm.Subject, "No") > 0 Then
xMailItem.SaveAs xFilePath & "\" & xFileName & ".html", olHTML
End If

End If
Exit Sub
End Sub