Search : in
By :

Outlook Macro - Creating folders

Last answer on Sep 21, 2009 3:18:05 pm BST Picazo, on Nov 21, 2008 2:58:10 pm GMT 
 Report this message to moderators

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

Configuration: Windows XP
Safari 525.19

Best answers for « Outlook Macro Creating folders » in :
[Outlook Express] Create letter paper / Insert background image Show [Outlook Express] Create letter paper / Insert background image The letter paper is an Outlook Express feature allowing sending emails with illustrations (images, text, etc.) as background. Creating letterhead under Outlook Express In the...
Creating a signature for (Outlook/Thunderbird) ShowCreating a signature for (Outlook/Thunderbird) Create a signature under Microsoft Outook Create a signature under Thunderbird Messaging software such as Outlook and Thunderbird allow you to stick a signature to your messages. This...
Creating an Autorun.inf file ShowCreating an Autorun file Allowing autorun Creating an Autorun.inf file Variations Customizing the icon Customizing the icon text Customizing the icon menu Allowing autorun Windows provide users with simple utility that runs...
Import contacts from Excel to Outlook ShowImport contacts from Excel to Outlook Issue Solution Issue How to import a contact list from Excel to Outlook and having them in a single list of contact , and not as splited items? Solution By using the software Excel 2000 and...
Download Public Folder Helpdesk ShowThis program can reinforce your Outlook e-mail performance. Public Folder Helpdesk can integrate into your Outlook mail an automatic support service for your customers. This program helps you to describe your customers’ problems by using...
File sharing in Windows XP ShowAdvantages File sharing involves making the content of one or more directories available through the network. All Windows systems have standard devices making it easy to share the content of a directory. However, file sharing may lead to security...

1

Pepper, on Dec 15, 2008 5:56:57 pm GMT

I too am interested a similar function. I will keep you informed if I get something close.

Reply to Pepper

2

Pepper, on Mar 3, 2009 12:23:43 pm GMT
  • +2

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.

Reply to Pepper

3

Pepper, on Mar 3, 2009 12:42:08 pm GMT

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

Reply to Pepper

4

yenny, on Sep 17, 2009 9:29:34 am BST

Hi, i am interested with this.
have u done it?
can you enlighten me a little bit please? :)

Reply to yenny

5

 Pepper, on Sep 21, 2009 3:18:05 pm BST
  • +2

It is not elegant but is good enough for my personal use. Let me know if you make enhancements that I might find useful. Thanks.

' File projects in their own subfolders
' Written by Bryce Pepper (bpepper@kcsouthern.com)
' Searches subject for a M or Z project number (must be between 4-6 digits)
' and files them in a project subfolder (create folder if one does not exist)
' added support for P & R projects 2009-03-03 B.Pepper
' added support for # to make Bill Z. happy 2009-03-04 B.Pepper

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

Sub Application_Startup()
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("Projects")
End Sub

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

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

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

Set colMatches = objRegEx.Execute(Item.Subject)
If colMatches.Count > 0 Then
For Each myMatch In colMatches
If Left$(myMatch.Value, 1) = "#" Then
folderName = "M" & Right$("00" & Mid$(myMatch.Value, 2), 6)
Else
folderName = Left$(myMatch.Value, 1) & Right$("00" & Mid$(myMatch.Value, 2), 6)
End If

If FolderExists(objDestinationFolder, folderName) Then
Set objProjectFolder = objDestinationFolder.Folders(folderName)
Else
Set objProjectFolder = objDestinationFolder.Folders.Add(folderName)
End If
Item.Move objProjectFolder
Next
End If

Set objProjectFolder = Nothing
End Sub

Function FolderExists(parentFolder As MAPIFolder, folderName As String)
Dim tmpInbox As MAPIFolder

On Error GoTo handleError
' If the folder doesn't exist, there will be an error in the next
' line. That error will cause the error handler to go to :handleError
' and skip the True return value

Set tmpInbox = parentFolder.Folders(folderName)
FolderExists = True
Exit Function
handleError:
FolderExists = False
End Function

Reply to Pepper