Excell 2000 - Select Folder (Get Folder Path)

Buttocks

New Member
Joined
Mar 10, 2003
Messages
6
:rolleyes: Here is an interesting one,

I have created a small application in Excel 2002 that utilises the following code:


With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show

For lngCount = 1 To .SelectedItems.Count
filename = .SelectedItems(lngCount)
Next lngCount

End With



(it allows the user to select a folder and returns the path of that folder)

However Application.FileDialog(msoFileDialogFolderPicker) is not supported in Excel 2000, Does anyone have a work around for this code?

I am aware I can use the GetOpenFilename Method to select a file in a folder and then determine the path from that but it is a bit messy. I could also ask the user to type the path in an input box but it is not what i am looking for.


Any solution/ideas greatly appreciated. :biggrin:


regards,

B
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
There are a number of ways to do this...here is one.

<pre><FONT COLOR="#007F00">'// Requires DLL version shell32.dll version 4.71 or later</FONT>
<FONT COLOR="#007F00">'// Operating systems</FONT>
<FONT COLOR="#007F00">'// Win2000, WinNT 4.0 with Internet Explorer 4.0, Win98, Win95 with Internet Explorer 4.0</FONT>

<FONT COLOR="#007F00">'Item Identifiers and Identifier Lists</FONT>
<FONT COLOR="#007F00">'The Shell uses object identifiers within the Shell's namespace.</FONT>
<FONT COLOR="#007F00">'All objects visible in the Shell (files, directories, servers, workgroups, and so on)</FONT>
<FONT COLOR="#007F00">'have unique identifiers among the objects within their parent folder.</FONT>
<FONT COLOR="#007F00">'These identifiers are called item identifiers, and they have the ****EMID data type</FONT>
<FONT COLOR="#007F00">'as defined in the Shtypes.h header file. An item identifier is a variable-length byte</FONT>
<FONT COLOR="#007F00">'stream that contains information that identifies an object within a folder.</FONT>
<FONT COLOR="#007F00">'Only the creator of an item identifier knows the content and format of the identifier.</FONT>
<FONT COLOR="#007F00">'The only part of an item identifier that the Shell uses is the first two bytes,</FONT>
<FONT COLOR="#007F00">'which specify the size of the identifier.</FONT>

<FONT COLOR="#007F00">'Each parent folder has its own item identifier that identifies it within its own parent folder.</FONT>
<FONT COLOR="#007F00">'Thus, any Shell object can be uniquely identified by a list of item identifiers.</FONT>
<FONT COLOR="#007F00">'A parent folder keeps a list of identifiers for the items it contains.</FONT>
<FONT COLOR="#007F00">'The list has the ITEMIDLIST data type. Item identifier lists are allocated by the Shell</FONT>
<FONT COLOR="#007F00">'and may be passed across Shell interfaces, such as IShellFolder. It is important to remember</FONT>
<FONT COLOR="#007F00">'that each identifier in an item identifier list is only meaningful within the context of its parent folder.</FONT>

<FONT COLOR="#007F00">'An application can set a shortcut's item identifier list by using the SetIDList method.</FONT>
<FONT COLOR="#007F00">'This method is useful when setting a shortcut to an object that is not a file, such as</FONT>
<FONT COLOR="#007F00">'a printer or disk drive. An application can retrieve a shortcut's item identifier list</FONT>
<FONT COLOR="#007F00">'by using the GetIDList method.</FONT>

<FONT COLOR="#007F00">'What is a PIDL? Why not just use file system paths?</FONT>
<FONT COLOR="#007F00">'A PIDL (Program ID list) is a way of identifying any namespace object.</FONT>
<FONT COLOR="#007F00">'You can also use paths to identify namespace objects, but only if they are part of</FONT>
<FONT COLOR="#007F00">'the file system. <FONT COLOR="#00007F">With</FONT> namespace objects that are not part of the file system, you must use PIDLs.</FONT>


<FONT COLOR="#00007F">Sub</FONT> BrowseForFolderShell()
<FONT COLOR="#00007F">Dim</FONT> objShell <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Object</FONT>
<FONT COLOR="#00007F">Dim</FONT> objFolder <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Object</FONT>
<FONT COLOR="#00007F">Dim</FONT> strFolderFullPath <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT>

<FONT COLOR="#00007F">Set</FONT> objShell = CreateObject("Shell.Application")
<FONT COLOR="#007F00">'oFolder = Shell.BrowseForFolder(Hwnd, sTitle, iOptions [, vRootFolder])</FONT>
<FONT COLOR="#00007F">Set</FONT> objFolder = objShell.BrowseForFolder(0, "Please select a folder", 0, "C:\") <FONT COLOR="#007F00">'SpecFolders.CSIDL_FAVORITES</FONT>

<FONT COLOR="#00007F">If</FONT> (Not objFolder <FONT COLOR="#00007F">Is</FONT> <FONT COLOR="#00007F">Nothing</FONT>) <FONT COLOR="#00007F">Then</FONT>
<FONT COLOR="#007F00">'// NB: <FONT COLOR="#00007F">If</FONT> SpecFolder= 0 = Desktop then ....</FONT>
<FONT COLOR="#00007F">On</FONT> <FONT COLOR="#00007F">Error</FONT> <FONT COLOR="#00007F">Resume</FONT> <FONT COLOR="#00007F">Next</FONT>
<FONT COLOR="#00007F">If</FONT> IsError(objFolder.Items.Item.path) <FONT COLOR="#00007F">Then</FONT> strFolderFullPath = <FONT COLOR="#00007F">CStr</FONT>(objFolder): <FONT COLOR="#00007F">GoTo</FONT> Here
<FONT COLOR="#00007F">On</FONT> <FONT COLOR="#00007F">Error</FONT> <FONT COLOR="#00007F">GoTo</FONT> 0
<FONT COLOR="#007F00">'// <FONT COLOR="#00007F">Is</FONT> it the Root Dir?...if so change</FONT>
<FONT COLOR="#00007F">If</FONT> Len(objFolder.Items.Item.path) > 3 <FONT COLOR="#00007F">Then</FONT>
strFolderFullPath = objFolder.Items.Item.path & Application.PathSeparator
<FONT COLOR="#00007F">Else</FONT>
strFolderFullPath = objFolder.Items.Item.path
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">If</FONT>
<FONT COLOR="#00007F">Else</FONT>
MsgBox "User cancelled": <FONT COLOR="#00007F">End</FONT>
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">If</FONT>

Here:
MsgBox "You selected:= " & strFolderFullPath, vbInformation, "ObjectFolder:= " & objFolder

<FONT COLOR="#00007F">Set</FONT> objFolder = <FONT COLOR="#00007F">Nothing</FONT>
<FONT COLOR="#00007F">Set</FONT> objShell = <FONT COLOR="#00007F">Nothing</FONT>

<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Sub</FONT>

</pre>
 
Upvote 0
Thank a million guys,

Just the examples I was looking for. Shell programming is a little beyond my capabilities but I will sure use the provided code in the examples given.


regards,

B :biggrin:
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top