Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook - rule to run a macro when mail goes into a specific folder

Status
Not open for further replies.

snowmantle

Programmer
Jun 20, 2005
70
GB
Hi,

Can i ask if anyone has any information on how to do the below?

I have so far created a macro to be run by the user to read all email items in a folder they choose and save all attachments in an explorer folder of their choice.

What i want to try and do is have a macro that runs as part of a rule.

When an email is received into a specific email folder, the rule needs to run the macro, find and save any attachments on the email and then move the email to another folder.

My current code runs through everything in the folder, which may cause problems if we receive emails before a previous run has been completed.

I am having trouble adapting it to work with a rule on a single email, was wondering if anyone could point me in the right direction?

Previous code is at: thread707-1079916
 
Or to make it easier, what i am looking to do is in VBA,

have a macro that will run when a new email enters a outlook inbox subfolder called "test", and the macro checks the new email for any attachments and then saves them in a folder on the hard drive and then moves the email to another outlook inbox subfolder called "complete".

I am also wondering if it possible to open the text file attachments using outlook and save the data inside into a table on an sql server database.
 
Hi, the below code i have setup to check any emails coming into a sub folder called "test", check for txt file attachments, save any to the "emailTest" folder and then move the email to a "complete" subfolder.

I was wondering if it is possible for Outlook to have a check in vba for a folder inside "emailTest" with the name of today's date in this format: yyyymmdd, ie. 20050623.

If there isnt one then it needs to create the folder before saving the attachment.

That way the attachments can be automatically sorted into the correct day's folder.

I am also wondering how stable this code will be, if it will work when possibly receiving 5 emails in a few seconds.

Code:
Option Explicit

Private WithEvents olSubItems As Items

Private Sub Application_Startup()
    
    Dim objNS As NameSpace
    Dim olInbox As MAPIFolder

    Set objNS = Application.GetNamespace("MAPI")
    Set olInbox = objNS.GetDefaultFolder(olFolderInbox)
    Set olSubItems = olInbox.Folders("test").Items
    Set objNS = Nothing
End Sub

Private Sub Application_Quit()
    Set olSubItems = Nothing
End Sub

Private Sub olSubItems_ItemAdd(ByVal Item As Object)
    Dim objNS As NameSpace
    Dim objInbox As MAPIFolder
    Dim objCompleteFolder As MAPIFolder
    Dim atmt As Attachment
    Dim fileName As String
    Dim i As Integer
    
    i = 1

    If Item.Class = olMail Then
            Set objNS = Application.GetNamespace("MAPI")
            Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
            Set objCompleteFolder = objInbox.Folders("complete")
            
            For Each atmt In Item.Attachments
                If Right(atmt.fileName, 4) = ".txt" Then
                    fileName = "P:\Imports\emailTest\" & _
                    Format(Item.CreationTime, "yyyymmdd_hhnn_") & i & "_" & atmt.fileName
                    atmt.SaveAsFile fileName
                    i = i + 1
                End If
            Next atmt
            If Not objCompleteFolder Is Nothing Then
                Item.Move objCompleteFolder
            End If

    End If
    
    Set objCompleteFolder = Nothing
    Set objInbox = Nothing
    Set objNS = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top