0

This is the code in Outlook that sets the rules in Outlook automatically to save the attachment (Excel) with date stamp:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat

    dateFormat = Format(Now, "yyyy-mm-dd H-mm")
    saveFolder = "c:\Users\abc1\Desktop\"
   For Each objAtt In itm.Attachments
      objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
      Set objAtt = Nothing
 Next

End Sub

Next step I want is to open the attachment once it's saved. Is that possible?

How about this one?

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
  Dim objAtt As Outlook.Attachment
  Dim saveFolder As String
  Dim dateFormat

      dateFormat = Format(Now, "yyyy-mm-dd H-mm")
      saveFolder = "c:\Users\abc1\Desktop\"
     For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
        Set objAtt = Nothing
   Next


Dim Shex As Object
   Set Shex = CreateObject("Shell.Application")
   tgtfile = "objatt"
   Shex.Open (tgtfile)

End Sub

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
D K
  • 33
  • 1
  • 8
  • It saves to my desktop and has been working fine. I need to open it on my desktop. Is it possible? – D K Apr 28 '15 at 17:36
  • You can use code from here to open any document in its default application: http://stackoverflow.com/questions/18921168/how-can-excel-vba-open-file-using-default-application – Tim Williams Apr 28 '15 at 18:06
  • Which one? Create object and then looking for tgtfile? How would I define the doc name - it would be different every time it gets downloaded. – D K Apr 28 '15 at 19:03
  • Yes - the `runit` answer with 5 votes. Save the full file path to a variable and pass it to `runit` – Tim Williams Apr 28 '15 at 19:05
  • would this be in excel or outlook? Not getting it. – D K Apr 28 '15 at 19:07
  • If you need to post code, *edit your question* and add it: it's unreadable in a comment. – Tim Williams Apr 28 '15 at 19:09
  • Tim, any updates on the above code? – D K Apr 29 '15 at 19:15

2 Answers2

1

Yes, it is possible. If you know exactly that you need to open Excel files you may use the Excel object model to get the job done. See How to automate Microsoft Excel from Visual Basic for more information. The Open method of the Workbooks class opens a workbook.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Try this:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)

    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat, FilePath As String

    dateFormat = Format(Now, "yyyy-mm-dd H-mm")
    saveFolder = "c:\Users\abc1\Desktop" '<<EDIT removed trailing \
    For Each objAtt In itm.Attachments
      FilePath = saveFolder & "\" & dateFormat & _
                  " " & objAtt.DisplayName
      objAtt.SaveAsFile FilePath
      runit FilePath
    Next

End Sub

Sub runit(FilePath as String)
   Dim Shex As Object
   Set Shex = CreateObject("Shell.Application")
   Shex.Open (FilePath)
End Sub

'Edit: I used this to test the code, since I'm not running
'      it from a rule
Sub Tester()

    Dim Msg As MailItem

    Set Msg = Application.ActiveInspector.CurrentItem

    saveAttachtoDisk Msg

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • first time I tried it kicked me out and operation failed..Next trial, it worked.although doesn't run the "runit" code. Since I can set up 1 rule for the incoming emails.. I don't think there's a way to create multiple rules. is there? – D K Apr 29 '15 at 20:19
  • There was an error in your original code which I hadn't spotted (adding an additional backslash onto `saveFolder`, which already had a trailing backslash). Try it with the fix. – Tim Williams Apr 29 '15 at 20:43
  • So the code does work with the additional backslash but unfortunately it opens the wrong attachment :) For example I've attached an excel file but it opens an image on the desktop. Haven't seen that before. – D K Apr 29 '15 at 21:22
  • If your mail item has embedded images then those will also be saved and opened. If you don't want that then you'll need to add some code to filter them out, perhaps by looking at the filename and checking the extension. – Tim Williams Apr 29 '15 at 22:30