Log in

View Full Version : [SLEEPER:] Edit code with : Send , Date , Sub , Receiver in code below . Thanks



nguyennpa
04-02-2021, 08:13 AM
Dear All .
Please help me add : Send , Date , Sub , Receiver in code below . Thanks

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
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "\MyEmails"
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 & ".msg", olMSG
End If
Exit Sub
End Sub

gmayor
04-03-2021, 12:10 AM
It is not clear what you are trying to do, but see http://www.vbaexpress.com/forum/showthread.php?68564-Save-selected-messages-VBA-does-not-save-replied-messages

nguyennpa
04-04-2021, 08:55 AM
Dear.
Please help me add this code in code below:

If LCase(StrFolderName) = "inbox" Then
StrFile = StrSaveFolder & "e-from_" & StrSenderName & "_" & StrReceived & "_re_" & StrName & ".msg"
ElseIf LCase(StrFolderName) = "sent items" Then
StrFile = StrSaveFolder & "e-to_" & StrTo & "_" & StrReceived & "_re_" & StrName & ".msg"

- I want file backup the same : e-from_Timy A_20211103_0514pm_re_FW Test Mail


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
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "\MyEmails"
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 & ".msg", olMSG
End If
Exit Sub
End Sub

gmayor
04-05-2021, 12:45 AM
Did you look at the code in the link? It does almost what you require and can do exactly what you require with only minor changes.
Your code was aimed at messages going into the inbox. If you want it to work for sent items, you are going to have to setup a second event related to the sent items folder.