Results 1 to 3 of 3

Thread: MS Office 2010, Save attachments to disk

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    2

    MS Office 2010, Save attachments to disk

    I have the following macro that I put into my outlook 2010. The macro runs fine however, I have to manually hit alt + f11 to run it. How can I convert this to a script so it can run automatically with a rule?

    THANKS!

    1 Code:
    1. Option Explicit
    2.  
    3. Sub Test()
    4.  
    5.  
    6.  
    7. Dim arg1 As String
    8. Dim arg2 As String
    9. Dim arg3 As String
    10.  
    11. arg1 = "Trekker"
    12. arg2 = "XLS"
    13. arg3 = "C:\Trekker"
    14. 'If you use "" it will create a date/time stamped
    15. 'folder for you in the "My Documents" folder.
    16. 'Note: If you use this "C:\Users\Ron\test" the folder must exist
    17.  
    18. SaveEmailAttachmentsToFolder arg1, arg2, arg3
    19.  
    20.  
    21. End Sub
    22.  
    23.  
    24.  
    25. Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As String, _
    26.                                  ExtString As String, DestFolder As String)
    27.     Dim ns As NameSpace
    28.     Dim Inbox As MAPIFolder
    29.     Dim SubFolder As MAPIFolder
    30.     Dim Item As Object
    31.     Dim Atmt As Attachment
    32.     Dim FileName As String
    33.     Dim MyDocPath As String
    34.     Dim I As Integer
    35.     Dim wsh As Object
    36.     Dim fs As Object
    37.     Dim createtime As String
    38.  
    39.     On Error GoTo ThisMacro_err
    40.  
    41.     Set ns = GetNamespace("MAPI")
    42.     Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    43.     Set SubFolder = Inbox.Folders(OutlookFolderInInbox)
    44.  
    45.     I = 0
    46.     ' Check subfolder for messages and exit of none found
    47.     If SubFolder.Items.Count = 0 Then
    48.         MsgBox "There are no messages in this folder : " & OutlookFolderInInbox, _
    49.                vbInformation, "Nothing Found"
    50.         Set SubFolder = Nothing
    51.         Set Inbox = Nothing
    52.         Set ns = Nothing
    53.         Exit Sub
    54.     End If
    55.  
    56.     'Create DestFolder if DestFolder = ""
    57.     If DestFolder = "" Then
    58.         Set wsh = CreateObject("WScript.Shell")
    59.         Set fs = CreateObject("Scripting.FileSystemObject")
    60.         MyDocPath = wsh.SpecialFolders.Item("mydocuments")
    61.         DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
    62.         If Not fs.FolderExists(DestFolder) Then
    63.             fs.CreateFolder DestFolder
    64.         End If
    65.     End If
    66.  
    67.     If Right(DestFolder, 1) <> "\" Then
    68.         DestFolder = DestFolder & "\"
    69.     End If
    70.  
    71.     ' Check each message for attachments and extensions
    72.     For Each Item In SubFolder.Items
    73.         For Each Atmt In Item.Attachments
    74.             If LCase(Right(Atmt.FileName, Len(ExtString))) = LCase(ExtString) Then
    75.                 createtime = Format(Item.CreationTime, "d-mmm-yy") & " " & Format(Item.CreationTime, "HhNnSs AM/PM")
    76.                 FileName = DestFolder & Item.SenderName & " " & createtime & " " & Atmt.FileName
    77.                 Atmt.SaveAsFile FileName
    78.                 I = I + 1
    79.             End If
    80.         Next Atmt
    81.     Next Item
    82.  
    83.     ' Show this message when Finished
    84.     If I > 0 Then
    85.         MsgBox "You can find the files here : " _
    86.              & DestFolder, vbInformation, "Finished!"
    87.     Else
    88.         MsgBox "No attached files in your mail.", vbInformation, "Finished!"
    89.     End If
    90.  
    91.     ' Clear memory
    92. ThisMacro_exit:
    93.     Set SubFolder = Nothing
    94.     Set Inbox = Nothing
    95.     Set ns = Nothing
    96.     Set fs = Nothing
    97.     Set wsh = Nothing
    98.     Exit Sub
    99.  
    100.     ' Error information
    101. ThisMacro_err:
    102.     MsgBox "An unexpected error has occurred." _
    103.          & vbCrLf & "Please note and report the following information." _
    104.          & vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
    105.          & vbCrLf & "Error Number: " & Err.Number _
    106.          & vbCrLf & "Error Description: " & Err.Description _
    107.          , vbCritical, "Error!"
    108.     Resume ThisMacro_exit
    109.  
    110. End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: MS Office 2010, Save attachments to disk

    there is an event, that runs when a mail item is received
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    2

    Re: MS Office 2010, Save attachments to disk

    westconn1,

    Thanks for your reply!

    I got the event:

    Private Sub Application_NewMail()
    Call Test
    End Sub

    However, is it possible for event handler to look at a particular subfolder and not the inbox? I do not want all my subroutines firing every time mail arrives. I only want it to process when it arrives to a particular subfolder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width