• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1211
  • Last Modified:

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
0
andyringle
Asked:
andyringle
1 Solution
 
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


0
 
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now