Chip Shadd
Chip Shadd

Reputation: 55

Outlook Macro intermittent permission error

I know nothing about VBA, I managed to put together a very simple macro that allows me to SaveAs emails via rules:

Sub sTXT(myMail As Outlook.MailItem)
 strname = myMail.Subject
 myMail.SaveAs "C:\Users\ss127w\Box Sync\maildump\" & strname & ".txt", OLTXT
End Sub
Sub Test()
    Call sTXT(ActiveExplorer.Selection(1))
End Sub

This has been working flawlessly for many many months, I've been exporting the same 2 types of emails that come in without issue. Well, today I tried to set up a new rule for a different email and this macro gives me a permission error:

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

I did not make any changes to the macro whatsoever. I started testing the macro on random other emails and noticed that it works on some and not others.

Now if I were to manually do a SaveAs, I'm able to save any email to the folder defined in the macro.

I suspect that the special characters in the subject line is causing issues, but I haven't done enough trial and error to really be able to conclude this (the subject line has a colon (:) in it).

Let me know what you think.

Upvotes: 0

Views: 840

Answers (1)

YowE3K
YowE3K

Reputation: 23974

I suspect that the special characters in the subject line is causing issues, but I haven't done enough trial and error to really be able to conclude this (the subject line has a colon (:) in it).

You are correct - the colon will cause issues.

If you try using Windows Explorer to edit a filename to include a colon, you will receive the message:

A file name can't contain any of the following characters: \ / : * ? " < > |

The best thing to do is convert any such characters in your subject to some other character, e.g. instead of

strname = myMail.Subject

you might use

strname = Replace(myMail.Subject, ":", "_")

to change any colons to underscores instead.

Or you could go for the full

strname = Replace( _
          Replace( _
          Replace( _
          Replace( _
          Replace( _
          Replace( _
          Replace( _
          Replace( _
          Replace(myMail.Subject, "\", "_"), _
                                  "/", "_"), _
                                  ":", "_"), _
                                  "*", "_"), _
                                  "?", "_"), _
                                  """", "_"), _
                                  "<", "_"), _
                                  ">", "_"), _
                                  "|", "_")

if you want to. Obviously, the choice of replacement character is up to you - you could even replace them with "" if you just want to get rid of the character.

Upvotes: 1

Related Questions