cjmitton
12-12-2012, 03:54 AM
I have the code which I've patched together to auto save emails
I used code from some other postings and works fine for individual selections but need to tweak it so it can handle mulitple selections (i.e. when your viewing the inbox using the ctrl or shift key to select a group of emails) to save them to a specific directory selected. I need this flexability for it to work for my users!
I've tried to do a loop but have failed as my VBA coding itsnt up to it! Any help is greatfully received.
Sub DFA_Save()
On Error GoTo Err_Msg
Dim objItem As Outlook.MailItem
Dim strPrompt As String, strName As String
Dim sreplace As String, mychar As Variant, strdate As String
Set objItem = Outlook.ActiveExplorer.Selection.Item(1)
If objItem.Class = olMail Then
If objItem.Subject <> vbNullString Then
strName = objItem.Subject
Else
strName = "No Subject"
End If
Dim strFolder As String
Dim strStartDir As Variant
strStartDir = "H:\"
strFolder = PickFolder(strStartDir)
If strFolder = "" Then Exit Sub
strTo = objItem.ReceivedByName
strFrom = objItem.SenderName
strdate = objItem.ReceivedTime
sreplace = "-"
For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
strName = Replace(strName, mychar, sreplace)
strdate = Replace(strdate, mychar, sreplace)
strFrom = Replace(strFrom, mychar, sreplace)
strTo = Replace(strTo, mychar, sreplace)
Next mychar
strPrompt = "Are you sure you want to save the item?"
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs strFolder & "\" & Format(Now, "yy-mm-dd") & " " & strFrom & " - " _
& strName & " Received By " & strTo & " on " & strdate & ".msg", olMSG
Else
MsgBox "You chose not to save."
End If
Else
Exit Sub
End If
Exit Sub
Err_Msg:
MsgBox "Item Not Selected or is not an Email", vbOKOnly, "Error"
End Sub
I know I need to move the part of selecting the folder to the beginning before I start the loop for each email selected but failed from there! I thought it best to put the code I started with / knows works!
I used code from some other postings and works fine for individual selections but need to tweak it so it can handle mulitple selections (i.e. when your viewing the inbox using the ctrl or shift key to select a group of emails) to save them to a specific directory selected. I need this flexability for it to work for my users!
I've tried to do a loop but have failed as my VBA coding itsnt up to it! Any help is greatfully received.
Sub DFA_Save()
On Error GoTo Err_Msg
Dim objItem As Outlook.MailItem
Dim strPrompt As String, strName As String
Dim sreplace As String, mychar As Variant, strdate As String
Set objItem = Outlook.ActiveExplorer.Selection.Item(1)
If objItem.Class = olMail Then
If objItem.Subject <> vbNullString Then
strName = objItem.Subject
Else
strName = "No Subject"
End If
Dim strFolder As String
Dim strStartDir As Variant
strStartDir = "H:\"
strFolder = PickFolder(strStartDir)
If strFolder = "" Then Exit Sub
strTo = objItem.ReceivedByName
strFrom = objItem.SenderName
strdate = objItem.ReceivedTime
sreplace = "-"
For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
strName = Replace(strName, mychar, sreplace)
strdate = Replace(strdate, mychar, sreplace)
strFrom = Replace(strFrom, mychar, sreplace)
strTo = Replace(strTo, mychar, sreplace)
Next mychar
strPrompt = "Are you sure you want to save the item?"
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs strFolder & "\" & Format(Now, "yy-mm-dd") & " " & strFrom & " - " _
& strName & " Received By " & strTo & " on " & strdate & ".msg", olMSG
Else
MsgBox "You chose not to save."
End If
Else
Exit Sub
End If
Exit Sub
Err_Msg:
MsgBox "Item Not Selected or is not an Email", vbOKOnly, "Error"
End Sub
I know I need to move the part of selecting the folder to the beginning before I start the loop for each email selected but failed from there! I thought it best to put the code I started with / knows works!