Ask your question Report

Outlook Macro - Creating folders [Solved/Closed]

Picazo - Last answer on Aug 26, 2011 9:35pm BST
Hello,
Could anyone guide me in creating an outlook macro that does the following:
I receive emails very frequently that have a "word" in the title of the email in the format of issue-xxxx, where xxxx is a 4 digit number. I have created a mailbox folder called issues. What I would like the macro to do is to find all emails with a string of the format issue-xxxx in the title and look for a folder under issues with that same name. If one is not found, then it should be created. The email should then be moved to that subfolder.
For example, suppose an email comes in with the word issue-1234. The macro, when run (hopefully via the toolbar), should find that email and check for a folder called issue-1234 under the issues folder and create it if it was not found. The email should then be moved to that issue-1234 folder.
I have not really done any macro programming in the past, so any help on how to get started would be appreciated. If you happen to have a macro that does this already, and want to share the code, that would be even better.
Thanks,
Picazo
Read more 

Outlook Macro - Creating folders »

16 replies
Answer
+5
moins plus
This is a rough start... still need to verify if folder already exists.

Dim WithEvents objInboxItems As Outlook.Items
Dim objDestinationFolder As Outlook.MAPIFolder

' Run this code to start your rule.
Sub StartRule()
Dim objNameSpace As Outlook.NameSpace
Dim objInboxFolder As Outlook.MAPIFolder

Set objNameSpace = Application.Session
Set objInboxFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objInboxItems = objInboxFolder.Items
Set objDestinationFolder = objInboxFolder.Parent.Folders("Testing")
End Sub

' Run this code to stop your rule.
Sub StopRule()
Set objInboxItems = Nothing
Set objDestinationFolder = Nothing
End Sub

' This code is the actual rule.
Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
Dim objProjectFolder As Outlook.MAPIFolder

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = False
' Search for email subjects that contain either an M or Z project number (M007439, Z6312)
objRegEx.Pattern = "([M,Z]\d{4,6})"

Set colMatches = objRegEx.Execute(Item.Subject)
If colMatches.Count > 0 Then
For Each myMatch In colMatches
' Set objProjectFolder = objDestinationFolder.Folders(myMatch.Value)
' If TypeName(objProjectFolder) = "Nothing" Then
Set objProjectFolder = objDestinationFolder.Folders.Add(myMatch.Value)
' End If
Item.Move objProjectFolder
Next
End If

Set objProjectFolder = Nothing
End Sub


To have the macro run every time Outlook starts, change the name of the subroutine from StartRule to Application_Startup or call the StartRule procedure that is in the Application_Startup procedure. The Application_Startup procedure must be located in the ThisOutlookSession module.

Answer
+1
moins plus
I changed my exists check to
If objProjectFolder Is Nothing Then

Now I need to pretty it up a bit. This should get you started in the right direction

bernhard - Jan 20, 2010 2:42pm GMT
Hi Pepper,

thanks for your wonderful script! It helped very much in finding my first steps for my own implementation.

To further improve your script I changed some small things and I want to add a new "feature" to it, and with common forces I think we can make it even better.
What I am trying to do is the following:
I have a big archive structure, where the different topics have an unique id. This unique id is also the name of the folder, BUT the folder can also have a description, e.g. "0000_this folder holds this content"

is there a way to have a wildcard appended to the foldername the msg will be moved?

thanks for your help
bernhard
Pepper - Mar 9, 2010 5:52pm GMT
I suppose you could modify the FolderExists routine to enumerate thru all the folders and use the regexp to find a partial match -- return true if found / false not found. Let me know how you come along with this.
Pepperbernhard - Mar 9, 2010 6:41pm GMT
Try this...

Function FolderExists(parentFolder As MAPIFolder, folderName As String)

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = False
objRegEx.Pattern = "(" & folderName & ".*)"

For Each F In parentFolder.Folders
Set colMatches = objRegEx.Execute(F.Name)
If colMatches.Count > 0 Then
FolderExists = True
folderName = colMatches(0).Value
Exit Function
End If
Next

FolderExists = False
End Function
Answer
+0
moins plus
I too am interested a similar function. I will keep you informed if I get something close.

Answer
+0
moins plus
Pst files are better saved in the Outlook 2007. This works effectively in receiving and sending mails and other important messages. For Vista operating system the PST Repair Vista tool works better. This can even perform a well recoveryof the lost, damaged and corrupted messages easily. To know about the features and work procedureof PST Repair Vista Software have a look on, http://www.pstrepairvista.com

Answer
+0
moins plus
i didn't get it? how to run

Marc - Apr 6, 2010 5:14am BST
Hi,

First of all clarify that I don't have any idea about Macros in outlook.
But I am trying to do exactly the same as Picazo, I have seen the post and I have tried to make it work, but it gives me compilation error on the first line.
I create a macro and paste the code, I am probably not doing it right. Can anyone let me know how to make it work?

Thanks !

Marc
D E - Jun 9, 2010 5:29pm BST
Hello All

I am actually looking for something link this
that works a bit diffrently is looks at the from "Display name" "Email Address"
Such as Mark Doe ,MDOE@doe.com"
and then sees is there is a folder under the inbox,doe.com,Mark Doe,
If so moves the mail into it, if not creates the folder structure and moves the mail.

Can anyone assist?
tom - Aug 26, 2011 9:35pm BST
Hi

You can use Inlook Time Management. it gives you the exact archiving functionality you look for.

www.inlooktm.com
This document entitled « Outlook Macro - Creating folders » from Kioskea.net (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.