none
Drag'n drop Vba outlook 2010

    Question

  • Hi all, sorry for the bad english


    Here I created an addin that allows me to do a management
    of attachments (wholesale).

    In fact there is myworry, I will take a file that is located on the desktop and I drag and drop in a new email I want a msgbox appears giving me the full path of it. Is this possible ? if so, how ?

    Other lesser concerns goal: when the user right click on the message body and paste it done, I would like if it's a file I stil displays the full path. Same questions.

    Here I hope you can help me. If you need clarification on anything Iam quite ready to give them to you.

    Sincerely,

    chewby79

    Tuesday, July 24, 2012 7:22 AM

Answers

  • if user creates new mails then you need to attach to NewInspector event on Inspectors collection in Application object. in that event handler grab mail from Inspector.CurrentItem and attach to beforeattachmentadd event on it. remember to release that event when inspector closes (close event)
    • Marked as answer by chewby79 Wednesday, July 25, 2012 9:51 AM
    Wednesday, July 25, 2012 8:01 AM

All replies

  • thanx, I find it too but i don't understand how it work (no example)
    Tuesday, July 24, 2012 7:48 AM
  • sample code:

    Dim WithEvents mail As MailItem
    Sub sample()
    Set mail = ThisOutlookSession.ActiveInspector.CurrentItem
    End Sub
    Private Sub mail_BeforeAttachmentAdd(ByVal Attachment As Attachment, Cancel As Boolean)
    End Sub

    but i just checked - filepath will be empty, so no luck here, this approach will not get you what you want.
    Tuesday, July 24, 2012 8:41 AM
  • thank you anyway for your help !

    I keep looking for a other solution.

    Tuesday, July 24, 2012 9:09 AM
  • The original file path is not available when the attachment is added to the Outlook item. You wouldn't be able to do what you wanted.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "chewby79" <=?utf-8?B?Y2hld2J5Nzk=?=> wrote in message news:ffce5939-55ec-4e55-b4e1-c8b1ccd45791...

    thank you anyway for your help !

    I keep looking for a other solution.


    Ken Slovak MVP - Outlook
    Tuesday, July 24, 2012 2:19 PM
    Moderator
  • Ok thanks for your reply !

    And if i just want to get the name when i put a file on my mail ? I just use the filename property  of the attachment in beforeAttachmendAdd ? but it doesn't work for me, no msgbox appear...

    I use visual studio 2010 and outlook 2010

    Tuesday, July 24, 2012 2:26 PM
  • What code are you using? You say no msgbox appears, how and where are you calling the msgbox?
     
    I tested this with some VBA code running in the ThisOutlookSessionClass, creating a mail item and dragging a random file to the email. Here's the code I tested with, which worked as expected:
     
    Dim WithEvents oM As Outlook.MailItem
     
    Sub initOM()
        Set oM = Application.CreateItem(olMailItem)
        oM.Subject = "test"
        oM.Display
    End Sub
     
    Private Sub oM_BeforeAttachmentAdd(ByVal Attachment As Attachment, Cancel As Boolean)
        MsgBox "oM_BeforeAttachmentAdd" & vbTab & Attachment.DisplayName & vbTab & Attachment.FileName
    End Sub
    The msgbox displayed this:
     
    oM_BeforeAttachmentAdd      RAZR_UG_Vzw_68016532001B.pdf              RAZR_UG_Vzw_68016532001B.pdf

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "chewby79" <=?utf-8?B?Y2hld2J5Nzk=?=> wrote in message news:3091cadd-4df6-4e48-b723-7cab42635b31...

    Ok thanks for your reply !

    And if i just want to get the name when i put a file on my mail ? I just use the filename property  of the attachment in beforeAttachmendAdd ? but it doesn't work for me, no msgbox appear...

    I use visual studio 2010 and outlook 2010


    Ken Slovak MVP - Outlook
    Tuesday, July 24, 2012 2:51 PM
    Moderator
  • thanx again.

    With your code i have an error at this point :

    Set oM = Application.CreateItem(olMailItem)

    I don't know if it has a link but i this code in my addin :

    public Class thisAddin

    Dim WithEvents myMail As Outlook.MailItem

    Private Sub oM_BeforeAttachmentAdd(ByVal Attachment As Attachment, Cancel As Boolean)
        MsgBox ("test & attachment.DisplayName))
    End Sub

    I think it miss something because i haven't thisOutlookSessionclass.

    Tuesday, July 24, 2012 3:13 PM
  • Where in the ThisAddin class did you place the code? What error did you get?

    The code I showed is VBA code, not Visual Studio code or code for a VSTO addin. Your post asked about VBA code, that's what I created. ThisOutlookSession is only available in the Outlook VBA project.
     
    Again, please show the code you're using so we don't have to guess. I assume it's actually VB.NET code and not VBA code.
     
    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "chewby79" <=?utf-8?B?Y2hld2J5Nzk=?=> wrote in message news:d09b6f22-9672-45d1-9b23-5ceb7b772030...

    thanx again.

    With your code i have an error at this point :

    Set oM = Application.CreateItem(olMailItem)

    I don't know if it has a link but i this code in my addin :

    public Class thisAddin

    Dim WithEvents myMail As Outlook.MailItem

    Private Sub oM_BeforeAttachmentAdd(ByVal Attachment As Attachment, Cancel As Boolean)
        MsgBox ("test & attachment.DisplayName))
    End Sub

    I think it miss something because i haven't thisOutlookSessionclass.


    Ken Slovak MVP - Outlook
    Tuesday, July 24, 2012 3:24 PM
    Moderator
  • Ha sorry, i was thinking that vba and vb for csto addin were the same...

    error that olmailitem doesn't exist

    that's my code :

    Public Class ThisAddIn
        Declare Function AjoutMail Lib "C:\Documents and Settings\Administrateur\Mes documents\RAD Studio\Projets\TestMessagesDLL\DLMDLL.dll" Alias "Ajouter_une_piece_jointe_a_un_mail" () As String
        Dim monInspecteur As Outlook.Inspectors
        Dim WithEvents myMail As Outlook.MailItem
        Public Shared WithEvents outApp As Outlook.Application

        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            monInspecteur = Me.Application.Inspectors
            outApp = Me.Application
        End Sub

    code to overibe attachment bouton:

        Protected Overrides Function CreateRibbonExtensibilityObject() As  _
    Microsoft.Office.Core.IRibbonExtensibility
            Return New Ribbon()
        End Function

        Public Sub myMail_BeforeAttachmentAdd(ByVal Attachment As Attachment, ByRef Cancel As Boolean) Handles monMail.BeforeAttachmentAdd
       
        MsgBox ("test "& attachment.DisplayName)
     
        End Sub


        Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

        End Sub
    End Class


    • Edited by chewby79 Tuesday, July 24, 2012 3:37 PM forgot something
    Tuesday, July 24, 2012 3:35 PM
  • In this case it appears that the code should be something like this, but it's hard to say as you still aren't showing the code you used to create the mail item within the context of your addin code:
     
        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            monInspecteur = Me.Application.Inspectors
            outApp = Me.Application
     
            myMail = outApp.CreateItem(Outlook.olMailItem)
            myMail.Subject = "test"
            myMail.Display()
        End Sub
        Public Sub myMail_BeforeAttachmentAdd(ByVal Attachment As Attachment, ByRef Cancel As Boolean) Handles myMail.BeforeAttachmentAdd
       
        MessageBox.Show("test "& Attachment.DisplayName)
     
        End Sub

    I'm not sure how your "myMail" became "monMail" other than translations back and forth to/from French without checking variable names for consistency.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "chewby79" <=?utf-8?B?Y2hld2J5Nzk=?=> wrote in message news:cc65b36c-3789-4123-8664-52d6729b493d...

    Ha sorry, i was thinking that vba and vb for csto addin were the same...

    error that olmailitem doesn't exist

    that's my code :

    Public Class ThisAddIn
        Declare Function AjoutMail Lib "C:\Documents and Settings\Administrateur\Mes documents\RAD Studio\Projets\TestMessagesDLL\DLMDLL.dll" Alias "Ajouter_une_piece_jointe_a_un_mail" () As String
        Dim monInspecteur As Outlook.Inspectors
        Dim WithEvents myMail As Outlook.MailItem
        Public Shared WithEvents outApp As Outlook.Application

        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            monInspecteur = Me.Application.Inspectors
            outApp = Me.Application
        End Sub

    code to overibe attachment bouton:

        Protected Overrides Function CreateRibbonExtensibilityObject() As  _
    Microsoft.Office.Core.IRibbonExtensibility
            Return New Ribbon()
        End Function

        Public Sub myMail_BeforeAttachmentAdd(ByVal Attachment As Attachment, ByRef Cancel As Boolean) Handles monMail.BeforeAttachmentAdd
       
        MsgBox ("test "& attachment.DisplayName)
     
        End Sub


        Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

        End Sub
    End Class



    Ken Slovak MVP - Outlook
    Tuesday, July 24, 2012 5:45 PM
    Moderator
  • you say: In this case it appears that the code should be something like this, but it's hard to say as you still aren't showing the code you used to create the mail item within the context of your addin code.

    but i haven't a code to create the mail, it's the user who create it and i try to catch it.

    olMailItem is not a member of Outlook.

    but thx again for your time.

    Wednesday, July 25, 2012 7:36 AM
  • if user creates new mails then you need to attach to NewInspector event on Inspectors collection in Application object. in that event handler grab mail from Inspector.CurrentItem and attach to beforeattachmentadd event on it. remember to release that event when inspector closes (close event)
    • Marked as answer by chewby79 Wednesday, July 25, 2012 9:51 AM
    Wednesday, July 25, 2012 8:01 AM
  • humm yeah ! ok if i understand what you say, i try this :

    Public Class ThisAddIn
    
        Dim WithEvents monInspecteur As Outlook.Inspectors
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            monInspecteur = Me.Application.Inspectors
        End Sub
    
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    
        End Sub
    
        Private Sub monInspecteur_NewInspector(ByVal Inspector As Outlook.Inspector) Handles monInspecteur.NewInspector
            Dim mailItem As Outlook.MailItem = TryCast(Inspector.CurrentItem, Outlook.MailItem)
        End Sub
    
        Public Sub mailItem_BeforeAttachmentAdd(ByVal Attachment As Outlook.Attachment, ByRef Cancel As Boolean)
            MsgBox("monMail_BeforeAttachmentAdd")
        End Sub
    
    End Class

    it doesn't work

    Wednesday, July 25, 2012 8:27 AM
  • part with inspectors you got OK, but where do you attach to events on mailitem?

    Wednesday, July 25, 2012 8:40 AM
  •  Dim mailItem As Outlook.MailItem = TryCast(Inspector.CurrentItem, Outlook.MailItem)

    i thought it was that.

    so i change the code like that:

    Public Class ThisAddIn
    
        Dim WithEvents monInspecteur As Outlook.Inspectors
    Dim WithEvents mailItem As Outlook.MailItem
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            monInspecteur = Me.Application.Inspectors
        End Sub
    
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    
        End Sub
    
        Private Sub monInspecteur_NewInspector(ByVal Inspector As Outlook.Inspector) Handles monInspecteur.NewInspector
            mailItem  = TryCast(Inspector.CurrentItem, Outlook.MailItem)
        End Sub
    
        Public Sub mailItem_BeforeAttachmentAdd(ByVal Attachment As Outlook.Attachment, ByRef Cancel As Boolean) Handles mailItem.BeforeAttachmentAdd
            MsgBox("monMail_BeforeAttachmentAdd")
        End Sub
    
    End Class

    it work !! for all those type attachment: drag'n drop, boutton Ctrl c Ctrl V etc..

    thank you very much


    Wednesday, July 25, 2012 8:54 AM
  • don't go dancing yet - remember that user can open multiple mails for edition in the same time - refactor your code to consider that :-)
    Wednesday, July 25, 2012 8:57 AM
  • I had not yet started to dance ^^.

    Humm i don't understand this point because it work if the user create 2 new mail...

    Wednesday, July 25, 2012 9:05 AM
  • have you verified that event launches for _both_ new mails? i mean - you overwrite your mailitem variable so i thought that events will no longer be raised for previous mailitem, but i admit i hardly know VBA. If it works for you - great :-)
    Wednesday, July 25, 2012 9:40 AM