Link to home
Start Free TrialLog in
Avatar of andyringle
andyringle

asked on

Ole Drag & Drop File Attachment From Outlook

I am trying to drag a file attachment from Outlook onto a VB form with a text box control and get the file name.  The code below works great with files from explorer.  

When I drag an attached file from Outlook I get Error #461 "the message Sepcified format doesn't match format of data".    Look to find the adjustment needed to drag an attached file from outlook and get the name of the file in the text box.  


Private Sub Form_Load()
    txt_File_Name.OLEDropMode = 1 ' Manual
End Sub

Private Sub txt_File_Name_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
   On Error GoTo errorLoadingFile
         
   'If Data.GetFormat(vbCFFiles) Then
        Dim fileName As String
        fileName = Data.Files(1)
        Me.txt_File_Name.Text = fileName
    'End If
   
    Exit Sub
   
errorLoadingFile:
    MsgBox fileName, vbExclamation, "Error Loading File"
End Sub
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andyringle
andyringle

ASKER

IT WORKED!!!! THANK YOU...THANK YOU...THANK YOU....

I followed the steps above and it worked like a champ.  Just a couple of notes:

1.  I did not get the Data.GetFormat(-16370) = True.  I got (Data.GetFormat(1) = True)
2.  I did not know how to get the entire file name with path.  I did a save locally and got what i was looking to get.

Private Sub Form_Load()
txt_File_Name.OLEDropMode = 1 ' Manual
End Sub

Private Sub txt_File_Name_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim oDragDrop As DragDropAttachment
Dim int_loop As Integer

Set oDragDrop = New DragDropAttachment
Set oDragDrop.Source = Data

'If (Data.GetFormat(-16370) = True) Then
If (Data.GetFormat(vbCFText) = True) Then   '-16370
    txt_File_Name = oDragDrop.Filename(0)  'THIS DOES NOT GIVE THE COMPLETE PATH.  JUST THE FILE NAME
    oDragDrop.SaveAttachment 0, "c:\temp\andy\attach.txt"  'I ADDED THIS TO SAVE THE ATTACHMENT.    
End If
   
End Sub