none
Save attachment from current message to disk and pass the filename to windows form RRS feed

  • Question

  • Hello,

    Apologies if the answer to this question is basic - I'm a bit of a newby !

    The Problem - I would like to be able to click a button on a ribbon (which I have created) and this will save the PDF attachement which is in the currently opened mail message to the local disk. The filename of that message will then be avaiable to pass to a windows form which will display the PDF with some other fields etc.

    I have tried a few things but just cant get it to work. In particular I can use this : http://msdn.microsoft.com/en-us/library/ms268754.aspx but that simply saves all the attachments in the inbox - I just want the one from the email which is open !

    Using visual studio 2010 and targetting Outlook 2007

    Help much appriciated !!!

    Tuesday, March 20, 2012 11:51 PM

Answers

All replies

  • Add your ribbon button using xml to inspector for reading mail ('Microsoft.Outlook.Mail.Read' in GetCustomUI(ribbonId)). On that click action grab Inspector from Control.Context and MailItem from Inspector.CurrentItem. Now iterate attachments to find your pdf and save it to disk calling SaveAsFile. Now how you would like to communicate with your winforms app is up to you, remoting, pipes, FileSystemWatcher, WCF...
    Wednesday, March 21, 2012 8:31 AM
  • If only 1 item is opened you can use ActiveInspector.CurrentItem to get a handle to the opened item and work with its Attachments collection. If more than 1 item is opened you can iterate the Inspectors collection and find the item that matches the one you want to work with.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ScotDoug" <=?utf-8?B?U2NvdERvdWc=?=> wrote in message news:a5e367a3-32fb-4b72-8186-533e54312d30...

    Hello,

    Apologies if the answer to this question is basic - I'm a bit of a newby !

    The Problem - I would like to be able to click a button on a ribbon (which I have created) and this will save the PDF attachement which is in the currently opened mail message to the local disk. The filename of that message will then be avaiable to pass to a windows form which will display the PDF with some other fields etc.

    I have tried a few things but just cant get it to work. In particular I can use this : http://msdn.microsoft.com/en-us/library/ms268754.aspx but that simply saves all the attachments in the inbox - I just want the one from the email which is open !

    Using visual studio 2010 and targetting Outlook 2007

    Help much appriciated !!!


    Ken Slovak MVP - Outlook
    Wednesday, March 21, 2012 1:52 PM
    Moderator
  • Thanks Damian

    Ok so iam more of a newby that you think !!!!

    If you were able to clarify how to add the ribbon button to the inspector that would be really helpful ! ('Microsoft.Outlook.Mail.Read' in GetCustomUI(ribbonId))

    Also how do I grab Inspector from Control.Context and MailItem from Inspector.CurrentItem

    I am Ok with itertating through the attachments and finding the PDF

    Cheers

    D

    I was starting with this to save locally - but I get " Error    3    'ActiveInspector' is not a member of 'System.Windows.Forms.Application'. "

    Sub getAttatchment()
            Dim myInspector As Outlook.Inspector
            Dim myItem As Outlook.MailItem
            Dim myAttachments As Outlook.Attachments

            myInspector = Application.ActiveInspector()
            If Not TypeName(myInspector) = "Nothing" Then
                If TypeName(myInspector.CurrentItem) = "MailItem" Then
                    myItem = myInspector.CurrentItem
                    myAttachments = myItem.Attachments
                    myAttachments.Item(1).SaveAsFile(Environ("HOMEPATH") _
                    & "\My Documents\" & myAttachments.Item(1).DisplayName)
               
                Else
                    MsgBox("Error.")
                End If
            End If
        End Sub

    • Edited by ScotDoug Wednesday, March 21, 2012 9:53 PM
    Wednesday, March 21, 2012 8:55 PM
  • write ribbon customization using xml as described here http://msdn.microsoft.com/en-us/library/aa338202(v=office.12).aspx

    as for your error about active inspector - you have a namespace conflict - Application class exists both in System.Windows.Forms and Microsoft.Office.Outlook.Interop.

    • Marked as answer by ScotDoug Thursday, March 22, 2012 3:05 PM
    Thursday, March 22, 2012 8:57 AM