-1

I completely lost my VBA touch, anyone that can help, I greatly appreciate it. For outlook desktop I want to a rule that automatically moves item to a folder, marks it as read and calls a script. ( I managed to do that )

How to enable script in outlook 2016: https://www.slipstick.com/outlook/rules/outlooks-rules-and-alerts-run-a-script/ For the subroutine to be seen by Rule Wizard, the argument must by type MailItem.

The script I want to run, is to save the message identified by the rule to disk as a txt file, and for that I am using:

In the module "ThisOutlookSession" the following code ( found it on Outlook VBA macro for saving emails copies in a local folder ) :

Public Sub SaveToDiskScript(Item As Outlook.MailItem)

Const olMsg As Long = 0 '0=Text format (.txt) -> https://learn.microsoft.com/en-us/office/vba/api/outlook.olsaveastype

    Dim m As MailItem
    Dim savePath As String


    Set m = Item

    savePath = "C:\Users\im.a.pretty.user\Desktop\StorageFolder\"
    savePath = savePath & m.Subject & Format(Now(), "yyyy-mm-dd-hhNNss")
    savePath = savePath & ".txt"


    m.SaveAs savePath, olMsg


End Sub

Thank you

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • @niton how to make it work, because at the moment it doesnt – PancakeSlapper Nov 15 '22 at 08:14
  • [What Do You Mean "It Doesn't Work"?](https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work) 1) It does nothing. 2) It moves the item but does not save. 3) There is a file permission error. Consider describing the rule. – niton Nov 15 '22 at 11:15
  • @niton version 1 - it does nothing – PancakeSlapper Nov 15 '22 at 11:35
  • macro security is set to enable all macros :( – PancakeSlapper Nov 15 '22 at 18:21
  • Set a breakpoint to verify the rule calls the code. To test further https://stackoverflow.com/questions/5029141/debugging-an-outlook-2007-script-fired-by-a-rule/58049467#58049467 – niton Nov 15 '22 at 18:50
  • There is likely a mistake in the rule. Regardless a rule with a move may lose the reference to the item and not save the attachment. One alternative is [ItemAdd on the inbox with the move in the code, after the save.](https://stackoverflow.com/questions/28052041/moving-outlook-message-with-specific-subject-to-subfolders) A second alternative is ItemAdd on the subfolder with the move in the rule. – niton Nov 16 '22 at 22:59

1 Answers1

0

The file path passed to the SaveAs method of the MailItem class is built based on the Subject line which may contain forbidden symbols:

savePath = "C:\Users\im.a.pretty.user\Desktop\StorageFolder\"
savePath = savePath & m.Subject & Format(Now(), "yyyy-mm-dd-hhNNss")
savePath = savePath & ".txt"

I'd recommend checking whether it contains any forbidden symbols before, see What characters are forbidden in Windows and Linux directory names? for more information.

Also you may try to specify a different folder without dots in the file path.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45