Option Explicit
'Behind ThisOutlookSession
Private oCBTools As Office.CommandBarPopup
Private oCBSaveMe As Office.CommandBarButton
Public WithEvents oSaveAs As Office.CommandBarButton
Private Sub SyncButton(btn As Office.CommandBarButton)
Set oSaveAs = btn
If btn Is Nothing Then
MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
End If
End Sub
Private Sub Application_MAPILogonComplete()
Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
If TypeName(oCBSaveMe) = "Nothing" Then
Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
End If
With oCBSaveMe
.BeginGroup = True
.Caption = "Save Email As..."
.Enabled = True
.Style = msoControlCustom
.Tag = "888"
.Visible = True
End With
Call SyncButton(oCBSaveMe)
End Sub
Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
On Error GoTo MyError
Dim oSel As Outlook.Selection
Dim oEmail As Outlook.MailItem
Dim sSub As String
100 Set oSel = Application.ActiveExplorer.Selection
110 If oSel.Class = olMail Then
120 Set oEmail = oSel
'Use the email subject as the filename
130 sSub = oEmail.Subject
'Parse out any invalid characters for a filename.
sSub = Replace(sSub, ":", vbNullString)
sSub = Replace(sSub, "*", vbNullString)
sSub = Replace(sSub, "/", vbNullString)
sSub = Replace(sSub, "\", vbNullString)
sSub = Replace(sSub, "?", vbNullString)
sSub = Replace(sSub, "<", vbNullString)
sSub = Replace(sSub, ">", vbNullString)
sSub = Replace(sSub, "|", vbNullString)
sSub = Replace(sSub, Chr(34), vbNullString)
'Add code for checking for duplicate filenames and implement a index number if so
140 oEmail.SaveAs "C:\MyEmails\" & sSub & ".txt", olTXT
Set oEmail = Nothing
MsgBox "Saved"
End If
Set oSel = Nothing
Exit Sub
MyError:
MsgBox Err.Number & " - " & Err.Description & vbNewLine & "On Line: " & Erl, vbOKOnly
End Sub