PDA

View Full Version : How do I Drag and Drop to a Userform Control?



Paul_Hossler
12-03-2009, 03:04 PM
I have a Userform with an Image Control on it (at least that's what I'm using right now) that opens vbModeless by a Workbook_Open.

I'm trying to figure out how to allow the user to Drag a file to the Image and Drop it there so that a Sub can process it. I'd really want it so that the file has to be dropped on the Image control.

Right now a command button opens a GetOpenFilename, the user gets the filename, and a Sub processes it. I was trying to polish that up a bit.

If I can get the file name I can pass it to the current Sub. If I get the file itself as an object, I can work with that also.


Paul

mikerickson
12-03-2009, 07:03 PM
By "drag a file..and drop it", do you mean drag the file's icon?

If so, where from where would the user drag the icon? The GetOpenFileName dialog box, the desktop or ???

Paul_Hossler
12-04-2009, 08:53 AM
The most common way I've seen would be that

1. the user has saved the file to their desktop.
2. the user opens the WB
3. the Userform Shows
4. the user then drags the file from their Desktop and drops it on the image control on the userform (what I'd like to do since it think it is more intuitive)
5. This gets a little fuzzy here, since it depends on what is available after the drag and drop, but the filename or the file itself is then handled/processed by existing VBA.

Thanks for any ideas

HTH

Paul

mikerickson
12-04-2009, 01:04 PM
This might be possible using Windows API's (about which this Mac user knows little).

My (limited) experience with Windows machines is that the common method of selecting file, is to choose from a list (similar to GetOpenFileName's dialog). Attaching files in Outlook. Dragging icons is only used for arranging folders.

My thought is that drag/drop may be more intuative, but it goes contrary to the user's "training" (though their other programs) that selecting a file is done by selecting from a list and pressing "OK".

Paul_Hossler
12-04-2009, 05:08 PM
I thought that I could use the BeforeDropOrPaste, but I can't figure out if it's what I'm lookin for, or how to use it:dunno




Private Sub Image1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Action As MSForms.fmAction, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
MsgBox "Here"


Stop

End Sub



Paul

mikerickson
12-04-2009, 07:01 PM
The first question is how to "grab" the icon from the desktop.

or, to be more detailed, how to get the filepath of the file whose icon is under the mouse when the button is pressed.

Then,
how to put the filepath into your routine when the button is raised while pointing at your image.

The difficulty that I see, (analogizing from my Mac) is that if you click on the desktop, focus is passed to the file handler portion of Windows (the Finder in Mac terms). At that point, your Excel VBA code is ignored and control passes to Windows.

If a Windows savy person could comment, they might see a way around this, but I don't see any way to do it, even on my Mac. And I wouldn't trust the stability of the result.