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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to browse for a file in Windows 7 (x64) ? 1

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
What's the best/easiest/correct/recommended way to browse for a file when running a script on a Windows 7 (x64) machine now that "UserAccounts.CommonDialog" is no longer available?
 
In case anyone is interested, here's an example of what I wound up using to browse for a file and return its name and path.

Code:
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

Originally posted by jeng02 at
He claimed to have problems with his script, but my example works fine for me.
 
From a DOS prompt

cd /
dir /s filename

The only problem is that you have to try it on all the disk drives. The advantage is that it doesn't look at zip files!
 
Others may find this useful, so I encourage you to take a few minutes, comment the code and post it to the FAQ section of the Forum.

PSC
[—] CCNP [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
This is strange. Before I started to comment the code, I copied the script I posted and ran it on my machine. I noticed that it would work fine with some file types (DOCX, PDF, ,XLS, and XLSX), but it would fail with others (DOC, TXT, and VBS). I never noticed this because I was focused on Excel files at the time.

The most puzzling thing to me is that both Excel file types worked fine, but the new Word file type worked while the old one did not. I'm going to try and figure this out before I post anything else.
 
Oops, didn't see the '&H4000' option which includes files.
 
I found a script here (it is the last code section on the page, scroll all the way down) that uses shell.application browse for folder method; but I do not have windows 7 to test on.

Perhaps this will work for you?
 
I decide to give up on BrowseForFolder. I've been playing around with the dialog from Word, instead. This works with any file type.

It sure would be nice if I could use something that is built into Windows. Is there no way to access the file open dialog from something like Notepad?

I was bored so I wrote up a basic script and put a bunch of comments in it in case anyone else is wanting to figure out how use Word for file browsing/selection. Let me know if you see any mistakes or have suggestions on how it could be done better.

Code:
[green]'set the type of dialog box you want to use
'1 = Open
'2 = SaveAs
'3 = File Picker
'4 = Folder Picker[/green]
Const msoFileDialogOpen = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")
Set WshShell = CreateObject("WScript.Shell")

[green]'where you want to start looking for files
'You could use a string like "C:\Somefolder\Somefolder\"
'I chose to use the desktop folder of whoever was running the script.  On Windows 7 it's "C:\Users\Username\Desktop\"
'Run "set" from a command prompt to see the available environment variables[/green]
strInitialPath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"

[green]'set the dialog box to open at the desired folder[/green]
objWord.ChangeFileOpenDirectory(strInitialPath)

With objWord.FileDialog(msoFileDialogOpen)
   [green]'set the window title to whatever you want[/green]
   .Title = "Select the file to process"
   [green]'I changed this to false because I'm working with one file at a time[/green]
   .AllowMultiSelect = False
   [green]'Get rid of any existing filters[/green]
   .Filters.Clear
   [green]'Show only the desired file types
   'for each desired group of file types, add a "Filters.Add" line with a different description and desired extensions
   'the dialog box will open using whichever filter is first
   'you can switch to a different filter from a drop-down list on the dialog box[/green]
   .Filters.Add "All Files", "*.*"
   .Filters.Add "Excel Files", "*.xls;*.xlsx"
   .Filters.Add "Text Files", "*.txt"
   .Filters.Add "Various Files", "*.xls;*.doc;*.vbs"
		 
   [green]'-1 = Open the file
   ' 0 = Cancel the dialog box
   '-2 = Close the dialog box
   'If objWord.FileDialog(msoFileDialogOpen).Show = -1 Then  'long form[/green]
   If .Show = -1 Then  [green]'short form[/green]
      [green]'Set how you want the dialog window to appear
      'it doesn't appear to do anything so it's commented out for now[/green]
      [green]'0 = Normal
      '1 = Maximize
      '2 = Minimize
      'objWord.WindowState = 2

      'the Word dialog must be a collection object
      'even though I'm using one file, I had to use a For/Next loop
      '"File" returns a string containing the full path of the selected file
	 
      'For Each File in objWord.FileDialog(msoFileDialogOpen).SelectedItems  'long form[/green]
      For Each File in .SelectedItems  [green]'short form[/green]
         [green]'Change the Word dialog object to a file object for easier manipulation[/green]
         Set objFile = fso.GetFile(File)
         [green]'Display the full path to the file[/green]
         WScript.Echo objFile.Path
         [green]'Display the path to the folder that the file is in[/green]
         WScript.Echo objFile.ParentFolder
         [green]'Display just the name of the file[/green]
         WScript.Echo objFile.Name
      Next    
   Else 
   End If
End With

[green]'Close Word[/green]
objWord.Quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top