Select Files Compatible w/Win7

August 15th, 2011 - 10:21 pm ET by Steve | Report spam
This has turned out to be strangely tedious. I have been using this
approach very nicely to invoke the common dialog object in WinXp to
help the user select files. This method fails in Windows 7 with an
"Class Is Not Licensed For Use" Error?
'########################################################
Dim sFullFilePath

sFullFilePath=ShowSelectFile("Find My File","C:\Basic_Projects", _
"(All Files) *.*|*.*|(Text Files) *.txt|*.txt")
WScript.Echo "Full File Path->" & sFullFilePath

Function ShowSelectFile(sDlgTitle, sInitDir, sFilter)

Const cdlOFNExplorer = &H80000
Const cdlOFNFileMustExist = &H1000
Const cdlOFNHideReadOnly = &H4
Const cdlOFNPathMustExist = &H800
Dim objCD

'Win7 Prompts Error When Attempting To Create This Object!
Set objCD = CreateObject("MSComDlg.CommonDialog")

With objCD
.MaxFileSize = 260
.Flags = cdlOFNExplorer OR cdlOFNFileMustExist OR _
cdlOFNHideReadOnly OR cdlOFNPathMustExist
.DialogTitle = sDlgTitle
.InitDir = sInitDir
.Filter=sFilter
End With

objCD.ShowOpen
wscript.echo objCD.FileName
ShowSelectFile=objCD.FileName
Set objCD = Nothing

End Function
'########################################################
'There are NUMEROUS examples/variations of using the WScript
Shell .BrowseForFolder method with the include files flags set ...
Const BIF_browseincludefiles = &H4000; unfortunately none of these
methods work well. They have a strange tendency to work only with
certain file types and fail with an "Object not found" error for many
other file types and most of my googling shows perplexed people ~
therefore I stopped persuing it.
Dim objShell
Dim strFileName
Dim strFilePath
Dim objFile

Set objShell = CreateObject("Shell.Application")
Set objFile = objShell.BrowseForFolder(0, "Choose a file:", &H4000)
strFileName = objFile.Title
strFilePath = objFile.self.Path

MsgBox "Just the file name: " & strFileName & vbcrlf & "The full path:
" & strFilePath

Set objFileName = Nothing
Set objFilePath = Nothing
Set objShell = Nothing
'########################################################
The user account commond dialog appoach fails in Win7 also with an
"ActiveXObject Can't Create Object" error. I thing this is a WinXP
only object.

Function GetFileName( sInitDir, sFileFilter )
' This function invokes a File Open Dialog and returns the
' full file path of the selected file as a string.

Dim objFileDlg

'Create a File Select Dialog Object
Set objFileDlg=CreateObject("UserAccounts.CommonDialog")

'Check arguments and use defaults when necessary
If Len(InitDir)=0 Then
'Set Default as: "My Documents"

objFileDlg.InitialDir=CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
Else
'Use the passed folder spec.
objFileDlg.InitialDir = sInitDir
End If

If Len(sFileFilter)=0 Then
'Default file filter is: "All Files"
objFileDlg.Filter = "All files|*.*"
Else
'Use the passed filter spec.
objFileDlg.Filter = sFileFilter
End If

'objFileDlg.DialogTitle="Some Kinda Title..."

'Invoke the File Open Dialog & Return The File Path
If objFileDlg.ShowOpen Then
GetFileName = objFileDlg.FileName
Else
GetFileName = ""
End If

End Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Is there a Windows 7 VBScript method which can reliably select the
full file path for any user selected filetype? Is this a Win7 Admin
permissions issue? I can't believe there would be an actual licensing
issue with the common dialog control object!

Thanks,

~Steve
email Follow the discussionReplies 4 repliesReplies Make a reply

Replies

#1 Mayayana
August 16th, 2011 - 08:44 am ET | Report spam
UserAccounts.CommonDialog is XP-only, as you
suspected.

MSComDlg.CommonDialog never was a good candidate.
It's an ActiveX control that wraps the system functions,
but it requires a license. Maybe you had Visual Studio 6
on XP and didn't put it on Win7? In any case, even if it
worked for you it wouldn't work for most people.

Microsoft has had strange, seemingly ambivalent behavior
around this topic. I still don't understand why they didn't
give browsing dialogs to WSH in the first place. I guess it's
just because they've never properly finished the WSH in the
first place. It was a thrown-together package to placate
sysyem admins who wanted something GUI to replace the
outdated command line. So Microsoft adapted the VBScript
that they were using in trying to destroy Netscape, added
file IO functionality, and called it a day. Much of what can
be done with WSH has been hacks ever since. (Shell.Application,
for instance, is just a happy accident.) To this day, what
Microsoft comes out with for scripting is still limited functionality
aimed not at fullscale functionality but rather at the limited
needs of system admins who spend their days tracking log
files, checking installed software, etc.

At this point I know of two methods for File Open. One is a very
simple method that uses a hidden IE instance. I think it still works
in Vista/7, but I'm not certain that there are no issues. The
other method, oddly, has been there all along, but is awkward
to call from VBS because it's actually an ActiveX control that
requires being sited on a window and is not marked safe for
scripting. But it seems to work on all systems from Win95 up. That
object is HTMLDlgHelper.HTMLDlgHelper

For a sample of both methods see here:

http://www.jsware.net/jsware/scripts.php5#classpk

On the same page are compiled controls for browsing dialogs,
if you want something like that. They have the disadvantage
that they have to be installed and registered on the PC where
you're using them, but if you were using MSComDlg.CommonDialog
before then I'm guessing you don't distribute any of your scripts.

-

| This has turned out to be strangely tedious. I have been using this
| approach very nicely to invoke the common dialog object in WinXp to
| help the user select files. This method fails in Windows 7 with an
| "Class Is Not Licensed For Use" Error?
| '########################################################
| Dim sFullFilePath
|
| sFullFilePath=ShowSelectFile("Find My File","C:\Basic_Projects", _
| "(All Files) *.*|*.*|(Text Files) *.txt|*.txt")
| WScript.Echo "Full File Path->" & sFullFilePath
|
| Function ShowSelectFile(sDlgTitle, sInitDir, sFilter)
|
| Const cdlOFNExplorer = &H80000
| Const cdlOFNFileMustExist = &H1000
| Const cdlOFNHideReadOnly = &H4
| Const cdlOFNPathMustExist = &H800
| Dim objCD
|
| 'Win7 Prompts Error When Attempting To Create This Object!
| Set objCD = CreateObject("MSComDlg.CommonDialog")
|
| With objCD
| .MaxFileSize = 260
| .Flags = cdlOFNExplorer OR cdlOFNFileMustExist OR _
| cdlOFNHideReadOnly OR cdlOFNPathMustExist
| .DialogTitle = sDlgTitle
| .InitDir = sInitDir
| .Filter=sFilter
| End With
|
| objCD.ShowOpen
| wscript.echo objCD.FileName
| ShowSelectFile=objCD.FileName
| Set objCD = Nothing
|
| End Function
| '########################################################
| 'There are NUMEROUS examples/variations of using the WScript
| Shell .BrowseForFolder method with the include files flags set ...
| Const BIF_browseincludefiles = &H4000; unfortunately none of these
| methods work well. They have a strange tendency to work only with
| certain file types and fail with an "Object not found" error for many
| other file types and most of my googling shows perplexed people ~
| therefore I stopped persuing it.
| Dim objShell
| Dim strFileName
| Dim strFilePath
| Dim objFile
|
| Set objShell = CreateObject("Shell.Application")
| Set objFile = objShell.BrowseForFolder(0, "Choose a file:", &H4000)
| strFileName = objFile.Title
| strFilePath = objFile.self.Path
|
| MsgBox "Just the file name: " & strFileName & vbcrlf & "The full path:
| " & strFilePath
|
| Set objFileName = Nothing
| Set objFilePath = Nothing
| Set objShell = Nothing
| '########################################################
| The user account commond dialog appoach fails in Win7 also with an
| "ActiveXObject Can't Create Object" error. I thing this is a WinXP
| only object.
|
| Function GetFileName( sInitDir, sFileFilter )
| ' This function invokes a File Open Dialog and returns the
| ' full file path of the selected file as a string.
|
| Dim objFileDlg
|
| 'Create a File Select Dialog Object
| Set objFileDlg=CreateObject("UserAccounts.CommonDialog")
|
| 'Check arguments and use defaults when necessary
| If Len(InitDir)=0 Then
| 'Set Default as: "My Documents"
|
|
objFileDlg.InitialDir=CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
| Else
| 'Use the passed folder spec.
| objFileDlg.InitialDir = sInitDir
| End If
|
| If Len(sFileFilter)=0 Then
| 'Default file filter is: "All Files"
| objFileDlg.Filter = "All files|*.*"
| Else
| 'Use the passed filter spec.
| objFileDlg.Filter = sFileFilter
| End If
|
| 'objFileDlg.DialogTitle="Some Kinda Title..."
|
| 'Invoke the File Open Dialog & Return The File Path
| If objFileDlg.ShowOpen Then
| GetFileName = objFileDlg.FileName
| Else
| GetFileName = ""
| End If
|
| End Function
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Is there a Windows 7 VBScript method which can reliably select the
| full file path for any user selected filetype? Is this a Win7 Admin
| permissions issue? I can't believe there would be an actual licensing
| issue with the common dialog control object!
|
| Thanks,
|
| ~Steve
|
|

Similar topics