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
andyringleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
To get the name of the attachment you will have to go to this link and download the OLELIB.TLB

http://www.mvps.org/emorcillo/en/code/vb6/index.shtml

Unzip the file and put the OLELIB.TLB file in your system32 directory. Then register it using:
regtlib olelib.tlb

Then in your project,goto Project>References and click browse. Browse to the olelib.tlb in the system32 folder to add it as a reference.

Then, on the same web page download the example called "Get dropped attachments from Outlook messages" and take the class module from that example and add it to your project. Once that is done then try this code on your form that contains the Textbox:

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

Set oDragDrop = New DragDropAttachment
Set oDragDrop.Source = Data
   
If (Data.GetFormat(-16370) = True) Then
    txt_File_Name = oDragDrop.Filename(0)
End If
   
End Sub


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andyringleAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.