We help IT Professionals succeed at work.

Ole Drag & Drop File Attachment From Outlook

andyringle
andyringle asked
on
Medium Priority
1,683 Views
Last Modified: 2012-08-14
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
Comment
Watch Question

Commented:
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


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.