How run a macro when i open my out look (with out going to macros and running)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seshu
    New Member
    • Nov 2006
    • 156

    How run a macro when i open my out look (with out going to macros and running)

    Hi Everybody
    I wrote a small macro in my outlook macros it is working fine but i want to run that automatically when i open my outlook and once after opening it
    i want to refresh my out look for every five minutes so that my macro will run for every five mins
    and here is the code what i wrote
    Code:
    Sub auto_open()
    Dim ns As NameSpace
     Dim Inbox As MAPIFolder
     Dim Item As Object
     Dim Atmt As Attachment
     Dim FileName As String
     Dim i As Integer
     
      Set ns = GetNamespace("MAPI")
     Set Inbox = ns.GetDefaultFolder(olFolderInbox)
     i = 0
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the Inbox.", vbInformation, _
               "Nothing Found"
        Exit Sub
     End If
      For Each Item In Inbox.Items
          For Each Atmt In Item.Attachments
           FileName = "C:\Email Attachments\" & Atmt.FileName
           Atmt.SaveAsFile FileName
           i = i + 1
        Next Atmt
     Next Item
     If i > 0 Then
        MsgBox "I found " & i & " attached files." _
           & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
           & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
     Else
        MsgBox "I didn't find any attached files in your mail.", vbInformation, _
        "Finished!"
    End If
    End Sub
    Regards
    Seshu
  • seshu
    New Member
    • Nov 2006
    • 156

    #2
    Some one please look into this

    Regards
    Seshu

    Comment

    • danp129
      Recognized Expert Contributor
      • Jul 2006
      • 323

      #3
      Assuming you only need to check for attachments at startup and everytime you get mail instead of every 5 minutes, this might work:

      Code:
      Private Sub Application_Startup()
          Call SaveAttachments()
      End Sub
      
      Private Sub Application_NewMail()
          Call SaveAttachments()
      End Sub
      
      Sub SaveAttachments()
       Dim ns As NameSpace
       Dim Inbox As MAPIFolder
       Dim Item As Object
       Dim Atmt As Attachment
       Dim FileName As String
       Dim i As Integer
       
        Set ns = GetNamespace("MAPI")
       Set Inbox = ns.GetDefaultFolder(olFolderInbox)
       i = 0
      If Inbox.Items.Count = 0 Then
          MsgBox "There are no messages in the Inbox.", vbInformation, _
                 "Nothing Found"
          Exit Sub
       End If
        For Each Item In Inbox.Items
            For Each Atmt In Item.Attachments
             FileName = "C:\Email Attachments\" & Atmt.FileName
             Atmt.SaveAsFile FileName
             i = i + 1
          Next Atmt
       Next Item
       If i > 0 Then
          MsgBox "I found " & i & " attached files." _
             & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
             & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
       Else
          MsgBox "I didn't find any attached files in your mail.", vbInformation, _
          "Finished!"
      End If
      End Sub

      Personally I would hate that script to run every time I got new mail or every 5 minutes. You might Google Application_New MailEx(ByVal EntryIDCollecti on As String) event and see if it's possible to only scan the new messages instead of the entire inbox on every new message, it probably is. You might also prompt the user if they want to scan their mailbox in the Application_Sta rtup sub.

      Comment

      • seshu
        New Member
        • Nov 2006
        • 156

        #4
        so you say that insted of downloading all the attachments again and again you are asking me to download only the new attachments thank you that was a good idea but the thing is i dont how to differentiate the new one with the old one programically but once after seeing your suggestuion if i write the save attachment code in new mail procedure we can down load only the new down load attachments is it true if so what is this id collection please give me a small explination


        Regards
        Seshu

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          (Wow! Thought I was looking in a mirror then, when I saw the avatar...;))

          What danp129 suggested was to "Google Application_New MailEx(ByVal EntryIDCollecti on As String) event and see if it's possible to only scan the new messages". Sounds like sound advice, to me.

          Comment

          • danp129
            Recognized Expert Contributor
            • Jul 2006
            • 323

            #6
            It should look something like this

            Code:
            Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
                Dim i&
                Dim itemID
                Dim oMail As MailItem
                Dim Atmt As Attachment
                Dim FileName$
                Dim arIDs() As String
                
                arIDs = Split(EntryIDCollection, ",")
            
                For Each itemID In arIDs
                    On Error Resume Next 'if item is not a "MailItem" following line would fail
                    Set oMail = Application.Session.GetItemFromID(itemID)
                    On Error GoTo 0
                    
                    If Not oMail Is Nothing Then
                        For Each Atmt In oMail.Attachments
                            FileName = "D:\Email Attachments\" & Atmt.FileName
                            Atmt.SaveAsFile FileName
                        Next Atmt
                        Set oMail = Nothing
                    End If
                Next
            End Sub

            Comment

            • Denburt
              Recognized Expert Top Contributor
              • Mar 2007
              • 1356

              #7
              FYI

              Version info was not supplied so I thought I would mention.

              Outlook 2003 and newer:
              Private Sub Application_New MailEx(ByVal EntryIDCollecti on As String)


              Older versions would use:

              Private Sub Application_New Mail()

              For more info you can visit MSDN, if you click on the following link it will provide you with more info and you can browse around to see examples and choose the version of your choice.


              In the article it mentions the following:
              "since the NewMail event does not provide any details about the arriving item(s), you would have to keep track of what items were previously processed and, if you are also running Rules Wizard rules, examine multiple folders for new items."
              Last edited by Denburt; May 3 '07, 05:48 PM. Reason: Add a link and more info

              Comment

              Working...