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

Drag and drop folders to determine contents

I thought I had this working until I tried to grab the My Documents folder onto my form.  When doing so, I get the following error:

Specified format does not match format of data.

Any other folder I've dragged onto the form has successfully listed the contents in my list box.  Is there something special I have to do to get folders like My DOcuments to work?

Below is my code:

Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, Y As Single)
On Error GoTo Error:
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    GetFiles fso.GetFolder(Data.Files(1))
    Set fso = Nothing
    Exit Sub
    Set fso = Nothing
    MsgBox "Not a valid folder.", vbOKOnly Or vbExclamation, "Error"
End Sub

Private Sub GetFiles(Folder)
    Dim SubFolders
    Dim SubFolder
    Dim Files As Files
    Dim File As File
    Set Files = Folder.Files
    If Files.Count <> 0 Then
        For Each File In Files
            List1.AddItem File.Name
    End If
    Set SubFolders = Folder.SubFolders
    If SubFolders.Count <> 0 Then
        For Each SubFolder In SubFolders
            GetFiles SubFolder
    End If
End Sub
  • 2
1 Solution
MetallicraftAuthor Commented:
Let me add...

I'm dragging from Windows Explorer.  And what I'm dragging is the My Documents in the file list directly under Desktop.  If, instead, I go to c:\documents and settings\<username>, I can drag THAT My Documents successfully onto my form.  
I don't think that will work because "My Documents" on your desktop is not an actual folder.You could try getting the location of the folder and passing it to your GetFiles sub.

    Dim Wshell As Object, Fold
    Set Wshell = CreateObject("WScript.Shell")
    Fold = Wshell.SpecialFolders("MyDocuments")

    Set fso = New FileSystemObject
    GetFiles fso.GetFolder(Fold)
    Set fso = Nothing
    Set Wshell = Nothing

MetallicraftAuthor Commented:
That works well.  Is there a way to tell if what's being dragged onto the for is the My Documents folder so I know what to do in that situation?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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