Hi,

I am having a problem with VBScript and the common dialog
(MSComDlg.CommonDialog).
What I am trying to do is build a dialog that allows me to select a target
directory. I am trying to copy this functionality from the Microsoft Word
2003
Dialog where a user can change the default path for documents. Tools -
Options -
File Locations - Documents - Modify. I know about the function
Shell.Application
- BrowseForFolder but it does look as good as the Office Dialog I want to
simulate.

So really in the code below I want to make it only browse directories and
remove the file filter (Files of type:) - how can this be done. As well
I want to make it only display directories and not list any files.

Enough waffle - here is the code.

Thanks,

Ward.



Public Const ofnAllowMultiSelect = &H200
Public Const ofnCreatePrompt = &H2000
Public Const ofnFileMustExist = &H1000
Public Const ofnHideReadOnly = &H4
Public Const ofnNoNetworkButton = &H20000
Public Const ofnNoReadOnlyReturn = &H8000
Public Const ofnNoTestfileCreate = &H10000
Public Const ofnNoValidate = &H100
Public Const ofnOverwritePrompt = &H2
Public Const ofnPathMustExist = &H800
Public Const ofnReadOnly = &H1
Public Const ofnShareAware = &H4000
Public Const ofnExplorer = &H80000
Public Const ofnNochangedir = &H8

Dim oComDlg
Dim path_name

Set oComDlg = CreateObject("MSComDlg.CommonDialog")

path_name = ""

With oComDlg
.DialogTitle = "Please select Target Directory"
.InitDir = "C:\"
.Filter = ""
.FilterIndex = 0
.MaxFileSize = 260
.FileName = "*.*"
.Flags = ofnPathMustExist Or ofnHideReadOnly Or ofnExplorer Or
ofnNoValidate Or ofnNochangedir
.ShowOpen
End With

path_name = Left(oComDlg.filename,InStrRev(oComDlg.filename,"\"))

If path_name = "" Then
MsgBox "User Chose Cancel"
wscript.quit 0
End If

msgbox "Path Name" & path_name

Re: Using MSComDlg.CommonDialog to make a Directory Browser in VBScrip by mr_unreliable

mr_unreliable
Wed Apr 12 11:55:25 CDT 2006

Ward, if all you want is a dialog to allow your user to
select a directory (sometimes called a folder), then
you can use the shell.app's "BrowseForFolder" dialog.

Here is some sample code:

--- <snip> ---
dim oShell
dim oFolder
dim oFolderItem

set oShell = CreateObject("Shell.Application")
set oFolder = oShell.BrowseForFolder(0,"Choose a Folder",0)
MsgBox("You Selected Folder: " & oFolder.Title)

set oFolderItem = oFolder.Items.Item
MsgBox("Folder Path: " & oFolderItem.Path)
--- </snip> ---

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)




Ward wrote:
> Hi,
>
> I am having a problem with VBScript and the common dialog
> (MSComDlg.CommonDialog).
> What I am trying to do is build a dialog that allows me to select a target
> directory. I am trying to copy this functionality from the Microsoft Word
> 2003
> Dialog where a user can change the default path for documents. Tools -
> Options -
> File Locations - Documents - Modify. I know about the function
> Shell.Application
> - BrowseForFolder but it does look as good as the Office Dialog I want to
> simulate.
>
> So really in the code below I want to make it only browse directories and
> remove the file filter (Files of type:) - how can this be done. As well
> I want to make it only display directories and not list any files.
>
> Enough waffle - here is the code.
>
> Thanks,
>
> Ward.
>
>
>
> Public Const ofnAllowMultiSelect = &H200
> Public Const ofnCreatePrompt = &H2000
> Public Const ofnFileMustExist = &H1000
> Public Const ofnHideReadOnly = &H4
> Public Const ofnNoNetworkButton = &H20000
> Public Const ofnNoReadOnlyReturn = &H8000
> Public Const ofnNoTestfileCreate = &H10000
> Public Const ofnNoValidate = &H100
> Public Const ofnOverwritePrompt = &H2
> Public Const ofnPathMustExist = &H800
> Public Const ofnReadOnly = &H1
> Public Const ofnShareAware = &H4000
> Public Const ofnExplorer = &H80000
> Public Const ofnNochangedir = &H8
>
> Dim oComDlg
> Dim path_name
>
> Set oComDlg = CreateObject("MSComDlg.CommonDialog")
>
> path_name = ""
>
> With oComDlg
> .DialogTitle = "Please select Target Directory"
> .InitDir = "C:\"
> .Filter = ""
> .FilterIndex = 0
> .MaxFileSize = 260
> .FileName = "*.*"
> .Flags = ofnPathMustExist Or ofnHideReadOnly Or ofnExplorer Or
> ofnNoValidate Or ofnNochangedir
> .ShowOpen
> End With
>
> path_name = Left(oComDlg.filename,InStrRev(oComDlg.filename,"\"))
>
> If path_name = "" Then
> MsgBox "User Chose Cancel"
> wscript.quit 0
> End If
>
> msgbox "Path Name" & path_name
>
>
>
>