Chad Belerique
Chad Belerique

Reputation: 49

Save email to Windows folder with subject as file name

I want specific emails (tickets) to be saved to a folder.

My search sent me to this post (Macro to save e-mail as text file, to be used in a rule).

When I try to make the output the Subject line I get

Outlook cannot complete the save due to a file permission error.

With SenderName or Time/Date it works.

Public Sub SaveEmail(msg As Outlook.MailItem)
    ' assume an email is selected
    Set msg = ActiveExplorer.Selection.Item(1)

    ' the 2nd and 3rd options work but the 1st does not
    ' msg.SaveAs "C:\" & msg.Subject & ".msg", olTXT
    ' msg.SaveAs "C:\" & Format(Now, "YYYYMMDDHHMMSS") & ".txt", olTXT
    msg.SaveAs "C:\" & msg.SenderName & ".txt", olTXT
 End Sub

Upvotes: 0

Views: 1954

Answers (1)

braX
braX

Reputation: 11755

Not all characters can be used in a Filename. Namely, these.

Asterisk (*)
Backslash (\)
Colon (:)
Angle brackets (< >)
Question mark (?)
Slash (/)
Plus sign (+)
Pipe (|)
Quotation mark (")

There are lots of places online to find a pre-written function which will remove or replace them. Here is one: Remove Illegal Characters from Filename

Upvotes: 1

Related Questions