PDA

View Full Version : Move email to exisiting folder, if folder does not exists create folder



ghengis
01-27-2017, 09:35 AM
Hi All,

Very new here and very new to VB scripting. Here is my problem, I expect it is a simple solution, but have yet to crack the code.

When an certain email with the subject 'A:B:C:F' email comes into the mail box it gets sorted into a folder called 'test'. Once it is in the 'test' I would like to perform the actions described below:

1. Pull the field 'B' from the subject
2. Check if there is a subfolder 'B'
3. If yes move email to that folder, if no create subfolder 'B' and move email to the folder.

Any suggestions? Tips? Resources?

Anything is much appreciated.

gmayor
01-27-2017, 10:24 PM
This is fairly straightforward - unless you are using Outlook 2016, when it becomes a bit more problematic.
Create or copy the following Macro in a new Outlook VBA module


Option Explicit

Sub FileMessage(Item As Outlook.MailItem)
Dim olNS As NameSpace
Dim vField As Variant
Dim strSubFolder As String
Dim olFolder As Folder
Dim olSubFolder As Folder
Dim bExists As Boolean
With Item
vField = Split(.Subject, Chr(58))
If Not UBound(vField) = 3 Then GoTo lbl_Exit
strSubFolder = Trim(vField(1))
Set olNS = GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
For Each olSubFolder In olFolder.folders
If olSubFolder.Name = strSubFolder Then
bExists = True
Exit For
End If
Next olSubFolder
If Not bExists Then
Set olSubFolder = olFolder.folders.Add(strSubFolder)
End If
Item.Move olSubFolder
End With
lbl_Exit:
Exit Sub
End Sub

Run the macro as a script from a rule that identifies the sender(s) of the message. It could probably be run as a rule on all incoming messages given the individual nature of the subject, but I would recommend limiting it if you can.

The problem with Outlook 2016 is that Outlook security may have blocked the use of Scripts attached to rules and the script command will be missing from the available Rules options. Should that occur, you can restore the command by means of a registry hack:

Close Outlook

Type Regedit in the Windows 10 taskbar search window.

Run Regedit and locate the key:

HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Security
Right click the right windows and create a new DWORD (32-bit) Value.

Name that new entry EnableUnsafeClientMailRules and set its value to 1

You may then still have an issue relating to security in which case see http://www.gmayor.com/create_and_employ_a_digital_cert.htm

ghengis
01-30-2017, 11:58 AM
Thanks! I am running outlook 2010. I ran the rule, in the inbox and on a sub folder. Not sure what could be the issue. But it is not creating a folder and sorting the email to it.

skatonni
01-30-2017, 01:55 PM
One possible source of trouble is the extra : in "Re: " and "Fw: ". You will have to strip these before Splitting the Subject.

If not the problem, you can set a breakpoint and try debugging the code. http://www.cpearson.com/excel/DebuggingVBA.aspx