Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I start the windows explorer and set it to My Doc 1

Status
Not open for further replies.

aclayborne

Programmer
May 3, 2000
49
US
From my program how do I start the windows explorer and set the open folder to My Documents.
Also how can I determine the last selected or currently open folder?
 
Opening the windows explorer is fairly simple. Here is and example to get to the windows\system32 directory:

Code:
Shell ("c:\windows\explorer c:\windows\system32",vbNormalFocus)

The trick is to go to the My Documents folder. I have some code for that but I don't know where it is off hand. I'll get that to you in a bit if no one beats me to it. As for the last selected or currently open folder, what do you mean? Are you talking about the recently opened files list? Or are you trying to return a value(a directory?) from explorer?
 
I need to send files to my documents or the currently opened directory.
 
In a bas module:
Code:
Option Explicit

Public Declare Function SHGetPathFromIDList Lib "shell32.dll" _
(ByVal pidl As Long, ByVal pszPath As String) As Long

Public Declare Function SHGetSpecialFolderLocation Lib _
"shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder _
As Long, pidl As ITEMIDLIST) As Long

Public Type ShortItemId
    cb As Long
    abID As Byte
End Type

Public Type ITEMIDLIST
    mkid As ShortItemId
End Type

Public Const CSIDL_PERSONAL = &H5
Then to open My Documents in an explorer window (need to add error handling):
Code:
Dim udtIDL As ITEMIDLIST
Dim lngRtn As Long          'Return value
Dim strFolder As String    'Location of My Documents folder
    
lngRtn = SHGetSpecialFolderLocation _
(Me.hWnd, CSIDL_PERSONAL, udtIDL)
If lngRtn = 0 Then
 strFolder = Space$(260)
 lngRtn = SHGetPathFromIDList( _
 ByVal udtIDL.mkid.cb, ByVal strFolder)
 If lngRtn Then
  strFolder = Left$(strFolder, _
  InStr(1, strFolder, Chr$(0)) - 1)
 End If
End If
lngRtn = Shell("Explorer.exe " & strFolder, vbNormalFocus)
I don't know the answer to your second question

Paul Bent
Northwind IT Systems
 
Thanks that's exactly what I needed for the first part. But once the explorer is open, if they create or select another folder, can I detect the currently open folder in explorer?
 
So are you really just wanting a common dialog from windows that allows the user to select a directory to save the file? That is done differently but isn't complicated. It can also default to the my documents folder if you want. Would that work?
 
If you want the user to browse for a folder and specify the file name use the Save File dialog in the MS Common Controls OCX.

If you just want the user to browse for a folder and return the path, use the SHBrowseForFolder API function. If you search here/Google there are lots of VB examples.

Paul Bent
Northwind IT Systems
 
Or use the Microsoft Shell Controls and Automation Library, which allows a somewhat shorter version:
[tt]
' Note that vDir can be either one of the ShellSpecialFolder constants (start with ssf), or a normal path string
Public Sub vbExplore(Optional vDir As Variant)
With New Shell32.Shell
.Explore vDir
End With
End Sub
[/tt]
And here's an example of the two ways of calling it:
[tt]
vbExplore "c:\"
vbExplore ssfDESKTOP



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top