Drag and drop folders to determine contents

Posted on 2005-04-28
Last Modified: 2012-05-05
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
Question by:Metallicraft

    Author Comment

    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.  
    LVL 28

    Accepted Solution

    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


    Author Comment

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now