By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

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

seshu
100+
P: 156
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
Expand|Select|Wrap|Line Numbers
  1. Sub auto_open()
  2. Dim ns As NameSpace
  3.  Dim Inbox As MAPIFolder
  4.  Dim Item As Object
  5.  Dim Atmt As Attachment
  6.  Dim FileName As String
  7.  Dim i As Integer
  8.  
  9.   Set ns = GetNamespace("MAPI")
  10.  Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  11.  i = 0
  12. If Inbox.Items.Count = 0 Then
  13.     MsgBox "There are no messages in the Inbox.", vbInformation, _
  14.            "Nothing Found"
  15.     Exit Sub
  16.  End If
  17.   For Each Item In Inbox.Items
  18.       For Each Atmt In Item.Attachments
  19.        FileName = "C:\Email Attachments\" & Atmt.FileName
  20.        Atmt.SaveAsFile FileName
  21.        i = i + 1
  22.     Next Atmt
  23.  Next Item
  24.  If i > 0 Then
  25.     MsgBox "I found " & i & " attached files." _
  26.        & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
  27.        & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
  28.  Else
  29.     MsgBox "I didn't find any attached files in your mail.", vbInformation, _
  30.     "Finished!"
  31. End If
  32. End Sub
Regards
Seshu
Apr 28 '07 #1
Share this Question
Share on Google+
6 Replies


seshu
100+
P: 156
Some one please look into this

Regards
Seshu
Apr 30 '07 #2

danp129
Expert 100+
P: 321
Assuming you only need to check for attachments at startup and everytime you get mail instead of every 5 minutes, this might work:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Application_Startup()
  2.     Call SaveAttachments()
  3. End Sub
  4.  
  5. Private Sub Application_NewMail()
  6.     Call SaveAttachments()
  7. End Sub
  8.  
  9. Sub SaveAttachments()
  10.  Dim ns As NameSpace
  11.  Dim Inbox As MAPIFolder
  12.  Dim Item As Object
  13.  Dim Atmt As Attachment
  14.  Dim FileName As String
  15.  Dim i As Integer
  16.  
  17.   Set ns = GetNamespace("MAPI")
  18.  Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  19.  i = 0
  20. If Inbox.Items.Count = 0 Then
  21.     MsgBox "There are no messages in the Inbox.", vbInformation, _
  22.            "Nothing Found"
  23.     Exit Sub
  24.  End If
  25.   For Each Item In Inbox.Items
  26.       For Each Atmt In Item.Attachments
  27.        FileName = "C:\Email Attachments\" & Atmt.FileName
  28.        Atmt.SaveAsFile FileName
  29.        i = i + 1
  30.     Next Atmt
  31.  Next Item
  32.  If i > 0 Then
  33.     MsgBox "I found " & i & " attached files." _
  34.        & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
  35.        & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
  36.  Else
  37.     MsgBox "I didn't find any attached files in your mail.", vbInformation, _
  38.     "Finished!"
  39. End If
  40. End Sub

Personally I would hate that script to run every time I got new mail or every 5 minutes. You might Google Application_NewMailEx(ByVal EntryIDCollection 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_Startup sub.
May 2 '07 #3

seshu
100+
P: 156
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
May 2 '07 #4

Expert 5K+
P: 8,434
(Wow! Thought I was looking in a mirror then, when I saw the avatar...;))

What danp129 suggested was to "Google Application_NewMailEx(ByVal EntryIDCollection As String) event and see if it's possible to only scan the new messages". Sounds like sound advice, to me.
May 2 '07 #5

danp129
Expert 100+
P: 321
It should look something like this

Expand|Select|Wrap|Line Numbers
  1. Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
  2.     Dim i&
  3.     Dim itemID
  4.     Dim oMail As MailItem
  5.     Dim Atmt As Attachment
  6.     Dim FileName$
  7.     Dim arIDs() As String
  8.  
  9.     arIDs = Split(EntryIDCollection, ",")
  10.  
  11.     For Each itemID In arIDs
  12.         On Error Resume Next 'if item is not a "MailItem" following line would fail
  13.         Set oMail = Application.Session.GetItemFromID(itemID)
  14.         On Error GoTo 0
  15.  
  16.         If Not oMail Is Nothing Then
  17.             For Each Atmt In oMail.Attachments
  18.                 FileName = "D:\Email Attachments\" & Atmt.FileName
  19.                 Atmt.SaveAsFile FileName
  20.             Next Atmt
  21.             Set oMail = Nothing
  22.         End If
  23.     Next
  24. End Sub
  25.  
May 3 '07 #6

Denburt
Expert 100+
P: 1,356
FYI

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

Outlook 2003 and newer:
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)


Older versions would use:

Private Sub Application_NewMail()

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.
http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx

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."
May 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.