0

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Could you give an example of a subject line? I’m mostly wondering if a subject line contains illegal characters and/or violates character limits in Windows file names. – JG7 Dec 19 '17 at 00:09
  • 1
    Aren't you passing a **MailItem** to this `Sub SaveEmail()`? Why did you change it to whatever selected? What if the selected isn't a mail item? – PatricK Dec 19 '17 at 02:01
  • https://www.experts-exchange.com/questions/28025657/Vba-Code-Eliminate-Illegal-Characters-from-a-filename.html – braX Dec 19 '17 at 13:52
  • @JoeyGrant "R1-Min User Service Ticket TICK00" plus 4 digits after the two zeros. PatricK Honestly, I'm not entirely sure. I kept playing with it until it worked with my rule. I assumed that because it only runs on an email that it'd work? Thanks for the responses! – Chad Belerique Dec 19 '17 at 18:11
  • @braX Didn't notice your message at first but it was exactly what I needed! Thanks! Now IDK how to mark your answer as correct since the "check" doesn't appear where I believe it should be. – Chad Belerique Dec 21 '17 at 19:34
  • @ChadBelerique I have added an answer for you to mark. – braX Dec 21 '17 at 19:42

1 Answers1

1

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

braX
  • 11,506
  • 5
  • 20
  • 33