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
Replies