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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Macro

Status
Not open for further replies.

dpgirl

Technical User
Apr 5, 2005
45
US
I am totally new to Visual Basic and VBA. I created a Sub in ThisOutlookSession that takes any unread messages in a particular folder in my Inbox and downloads the attachment to a couple of places on the network. When I run the macro manually, it runs fine. When I let it run on its own in Outlook, nothing happens. Any idea what I'm doing wrong? Below is my code:

Code:
Option Explicit   

Sub SaveAttachmentsToFolder()
' This Outlook macro checks a particular subfolder in the Inbox
' for any unread messages and saves the attachment from each message to disk.
    On Error GoTo SaveAttachmentsToFolder_err

' Declare variables
    Dim objOutlook As Outlook.Application
    Dim objEmail As Outlook.MailItem
    Dim ns As NameSpace
    Dim Inbox2 As MAPIFolder
    Dim SubFolder3 As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim FileName2 As String
    Dim SenderEmailAddress As String
    Dim Subject As String
    
    Dim i As Integer

    Set objOutlook = CreateObject("Outlook.application")
    Set objEmail = objOutlook.CreateItem(olMailItem)
    Set ns = GetNamespace("MAPI")
    Set Inbox2 = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder3 = Inbox2.Folders("DC Cookie Files")


    i = 0
' Check subfolder for messages and exit if none found
    If SubFolder3.Items.Count = 0 Then
        Exit Sub
    End If

    ResumeClickYes    'Turns on ClickYes in separate module

' Check each message for attachments
    For Each Item In SubFolder3.Items
        If Item.UnRead = True And Left(Item.Subject, 18) = "Cookie Data" And Item.SenderEmailAddress = "reports@zzz.net" Then
      For Each Atmt In Item.Attachments
           ' Check filename of each attachment and save if it has "csv" extension
            If Right(Atmt.FileName, 3) = "csv" Then
                FileName = "\\server02\TransferIn\" & Atmt.FileName
                Atmt.SaveAsFile FileName
                FileName2 = "\\server02\Online\" & Atmt.FileName
                Atmt.SaveAsFile FileName2
                i = i + 1
            End If
       Next Atmt
        
           Item.UnRead = False
         End If
    Next Item
    
' Send summary message
    If i > 0 Then
        With objEmail
        .To = "john.smith@mycompany.com"
        .Subject = i & " Cookie File(s) Processed"
        .Send
        End With
        Set objEmail = Nothing

    Else
        Set objEmail = Nothing

    End If
    
    
' Clear memory
SaveAttachmentsToFolder_exit:
    Set objOutlook = Nothing
    Set objEmail = Nothing
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Set Inbox2 = Nothing
    Set SubFolder3 = Nothing
    
    SuspendClickYes     'Turns off ClickYes
    
    Exit Sub


' When Error Occurs, Send Message
SaveAttachmentsToFolder_err:
        With objEmail
        .To = "john.smith@mycompany.com"
        .Subject = "Error Occurred with Cookie File Processing"
        .Body = "Error Description: " & Err.Description
        .Send
        End With
        Set objEmail = Nothing
    
    Resume SaveAttachmentsToFolder_exit
    
    SuspendClickYes     'Turns off ClickYes
End Sub
 
I guess my definition of "running on its own" is that I've saved the project in Outlook and I have Outlook open running. Isn't the macro supposed to run automatically while I have Outlook open? Or is that a wrong assumption?
 
Have a look at events in Help. Particularly the NewMail event and the Startup event. If it is inspecting folders for unread items you surely only need it to run once or on demand. From there you want it to run each time a new email arrives?



Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top