Alan McGowan
Senior Member
- Outlook version
- Outlook 2013 64 bit
- Email Account
- Exchange Server
The organisation I work for has a requirement to save incoming and outgoing emails in MSG format in a standard filenaming format to different locations on a server (different projects have different locations on server). As I can received hundreds of emails per day I am trying to develop some code to make this as simple as possible although I am very new to writing code and have managed to scrounge bits and pieces from the internet. One of the functions I have partially working is a prompt to save a sent email to the server folder. At present I have a userform that opens using Application_ItemSend. On this userform is a listbox which has the paths to the different folders on the server and I have a button that runs a routine that saves the email in MSG format in the selected folder. If this is a new email (i.e. not a reply of a forward) then it works fine. The problem is when I reply to a message or forward a message it is not my message that is being saved its the original message that I'm forwarding or replying to. I'm sure the problem is becase at the point the routine is ran the message hasn't actually been sent at that point. The code attached to my button is below.
I would really appreciate any help in solving this.
Sub SaveSent2_test()
Dim lngC As Long
Dim msgItem As Outlook.MailItem
Dim strPath As String
On Error Resume Next
strPath = UserForm3.TextBox1.Value
On Error GoTo 0
If strPath = "" Then Exit Sub
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
If TypeName(Application.ActiveWindow) = "Explorer" Then
' save selected messages in Explorer window
If CBool(ActiveExplorer.Selection.Count) Then
With ActiveExplorer
For lngC = 1 To .Selection.Count
Set msgItem = .Selection.Item(lngC)
If .Selection(lngC).Class = olMail Then
MsgSaverTest strPath, .Selection(lngC)
End If
Next lngC
End With
End If
ElseIf Inspectors.Count Then
' save active open message
If ActiveInspector.CurrentItem.Class = olMail Then
MsgSaverTest strPath, ActiveInspector.CurrentItem
End If
End If
End Sub
Private Sub MsgSaverTest(strPath As String, msgItem As Outlook.MailItem)
Dim intC As Integer
Dim intD As Integer
Dim intE As Integer
Dim intF As Integer
Dim strMsgSubj As String
Dim strMsgTo As String
Dim senton As String
Dim senttime As String
senton = Format(Date, "yyyy-mm-dd")
senttime = Time
strMsgSubj = msgItem.Subject
strMsgTo = msgItem.To
' Clean out characters from Subject which are not permitted in a file name
For intC = 1 To Len(strMsgSubj)
If InStr(1, ":<&>""", Mid(strMsgSubj, intC, 1)) > 0 Then
Mid(strMsgSubj, intC, 1) = "-"
End If
Next intC
For intC = 1 To Len(strMsgSubj)
If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
Mid(strMsgSubj, intC, 1) = "_"
End If
Next intC
' Clean out characters from Sender Name which are not permitted in a file name
For intD = 1 To Len(strMsgTo)
If InStr(1, ":<&>""", Mid(strMsgTo, intD, 1)) > 0 Then
Mid(strMsgTo, intD, 1) = "-"
End If
Next intD
For intD = 1 To Len(strMsgTo)
If InStr(1, "\/|*?", Mid(strMsgTo, intD, 1)) > 0 Then
Mid(strMsgTo, intD, 1) = "_"
End If
Next intD
' Clean out characters from Date which are not permitted in a file name
For intE = 1 To Len(senton)
If InStr(1, "\/|*?", Mid(senton, intE, 1)) > 0 Then
Mid(senton, intE, 1) = "-"
End If
Next intE
For intF = 1 To Len(senttime)
If InStr(1, "\:/|*?", Mid(senttime, intF, 1)) > 0 Then
Mid(senttime, intF, 1) = "."
End If
Next intF
' add date to file name
strMsgSubj = senton & " " & senttime & " " & "[To " & strMsgTo & "]" & " " & strMsgSubj & ".msg"
msgItem.SaveAs strPath & strMsgSubj
If UserForm1.TextBox3.Value = "YES" Then
Set msgItem.SaveSentMessageFolder = _
Session.GetDefaultFolder(olFolderDeletedItems)
End If
Set msgItem = Nothing
UserForm3.Hide
End Sub
I would really appreciate any help in solving this.
Sub SaveSent2_test()
Dim lngC As Long
Dim msgItem As Outlook.MailItem
Dim strPath As String
On Error Resume Next
strPath = UserForm3.TextBox1.Value
On Error GoTo 0
If strPath = "" Then Exit Sub
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
If TypeName(Application.ActiveWindow) = "Explorer" Then
' save selected messages in Explorer window
If CBool(ActiveExplorer.Selection.Count) Then
With ActiveExplorer
For lngC = 1 To .Selection.Count
Set msgItem = .Selection.Item(lngC)
If .Selection(lngC).Class = olMail Then
MsgSaverTest strPath, .Selection(lngC)
End If
Next lngC
End With
End If
ElseIf Inspectors.Count Then
' save active open message
If ActiveInspector.CurrentItem.Class = olMail Then
MsgSaverTest strPath, ActiveInspector.CurrentItem
End If
End If
End Sub
Private Sub MsgSaverTest(strPath As String, msgItem As Outlook.MailItem)
Dim intC As Integer
Dim intD As Integer
Dim intE As Integer
Dim intF As Integer
Dim strMsgSubj As String
Dim strMsgTo As String
Dim senton As String
Dim senttime As String
senton = Format(Date, "yyyy-mm-dd")
senttime = Time
strMsgSubj = msgItem.Subject
strMsgTo = msgItem.To
' Clean out characters from Subject which are not permitted in a file name
For intC = 1 To Len(strMsgSubj)
If InStr(1, ":<&>""", Mid(strMsgSubj, intC, 1)) > 0 Then
Mid(strMsgSubj, intC, 1) = "-"
End If
Next intC
For intC = 1 To Len(strMsgSubj)
If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
Mid(strMsgSubj, intC, 1) = "_"
End If
Next intC
' Clean out characters from Sender Name which are not permitted in a file name
For intD = 1 To Len(strMsgTo)
If InStr(1, ":<&>""", Mid(strMsgTo, intD, 1)) > 0 Then
Mid(strMsgTo, intD, 1) = "-"
End If
Next intD
For intD = 1 To Len(strMsgTo)
If InStr(1, "\/|*?", Mid(strMsgTo, intD, 1)) > 0 Then
Mid(strMsgTo, intD, 1) = "_"
End If
Next intD
' Clean out characters from Date which are not permitted in a file name
For intE = 1 To Len(senton)
If InStr(1, "\/|*?", Mid(senton, intE, 1)) > 0 Then
Mid(senton, intE, 1) = "-"
End If
Next intE
For intF = 1 To Len(senttime)
If InStr(1, "\:/|*?", Mid(senttime, intF, 1)) > 0 Then
Mid(senttime, intF, 1) = "."
End If
Next intF
' add date to file name
strMsgSubj = senton & " " & senttime & " " & "[To " & strMsgTo & "]" & " " & strMsgSubj & ".msg"
msgItem.SaveAs strPath & strMsgSubj
If UserForm1.TextBox3.Value = "YES" Then
Set msgItem.SaveSentMessageFolder = _
Session.GetDefaultFolder(olFolderDeletedItems)
End If
Set msgItem = Nothing
UserForm3.Hide
End Sub