Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I just wanted to say THANKS for the forum. The knowledge I gain from your site is invaluable..."

Geography

Where in the world do Tek-Tips members come from?

Odd error using BrowseForFolder

frumpus (Programmer)
25 May 12 16:49
All I'm trying to do is get an 'open file' dialog to work. In XP it was the easiest thing in the world but in Windows 7 (64 bit) the old dialogs don't exist. From some googling I've found this can supposedly be done with BrowseForFolder which has an option to include browsing for files.

This code should work from what I understand. It successfully opens the dialog and starts browsing in the correct folder. However, when I select the file I want and click 'OK' it returns an "unspecified error - 80004005" on the 'Set oFolder' line. From what I can find, that is often a permissions related error, but I am an admin on the machine and the owner of the directory I'm browsing in. Any thoughts what I'm missing here or will this just not do what I want?

CODE

Const MY_DOCUMENTS = &H5& Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(MY_DOCUMENTS) Set objFolderItem = objFolder.Self strPath = objFolderItem.Path strPath = strPath & "\BlackBoardBatch" Const INCLUDEFILES = &H4000 ' Browsing for Everything Const strTitle = "Select a merge_data file... " vOptions = INCLUDEFILES Set oFolder = objShell.BrowseForFolder(&H0, strTitle, vOptions, strPath) If (oFolder Is Nothing) then 'clicked cancel WScript.Quit ElseIf (UCase(TypeName(oFolder)) = "FOLDER") Then sResult = oFolder.ParentFolder.ParseName(oFolder.Title).Path msgBox(sResult) End If

A good write-up on this method can be found Here.

Any suggestions would be appreciated.
Geates (Programmer)
29 May 12 9:31
It works expectedly for me.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer

frumpus (Programmer)
29 May 12 13:08
Hmm...

What I don't understand is that I can (in vbscript) create folders and files in the same directory with no problems. It is only the BrowseForFolder object that is having issues.
Geates (Programmer)
29 May 12 14:15
Perhaps the My Documents namespace is "corrupt". Do the script run on another computer?

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer

guitarzan (Programmer)
30 May 12 14:19
Searches for BrowseForFolder shows that it is very old, and buggy for XP ( let alone Win7). You might try trapping that particular error and ignoring it. The error number that seems to be thrown is -2147467259

So if you surround the BrowseForFolder command like below, it may work.

CODE

On Error Resume Next Set objFolder = objShell.BrowseForFolder(&H0, strTitle, vOptions, strPath) If Not ((Err.number = 0) Or (Err.number = -2147467259)) Then Wscript.echo "Got error #" & Err.number & ": " & Err.description Wscript.quit End If On Error Goto 0

Also not that TypeName seems to return "FOLDER3" for me, not "FOLDER"
frumpus (Programmer)
4 Jun 12 12:50
@Geates: It does run on other computers. I've tried it on mine by giving it a different path just using "C:\somefolder" in the 4th parameter and avoiding the namespace usage but still get the same error. Any clues how I might be able to fix this without reinstalling win7?

@guitarzan: Ignoring the error didn't work. I end up with no oFolder object at all. :-/

guitarzan (Programmer)
4 Jun 12 15:00
Yes, playing further shows it works for some file types (like pdf or doc) and not others (like txt or jpg). Very flaky behavior. My guess is, that's just how it works. I do not believe that your OS is corrupt.

Some quick web searches I did don't seem to give much hope for doing this in vbscript/Win7. You may look into using an HTA file (which can run vbscript) and using <INPUT TYPE="FILE"> to select the file.
frumpus (Programmer)
4 Jun 12 16:46
Interesting. It does work if I select a .pdf file. No luck with .doc or anything else I've tried so far.

I don't understand why they got rid of the old CommonDialog method from XP :-/

frumpus (Programmer)
4 Jun 12 17:19
HTA looks useful but I'm not seeing how INPUT TYPE="FILE" will help me browse for the input file with a dialog box.

Even in HTA this seems to need the old common dialog and only works in XP.

http://technet.microsoft.com/en-us/library/77941da4-4b4e-4dfd-8304-8f451b8c253e

Did you find an example of this somewhere?
guitarzan (Programmer)
4 Jun 12 19:42
The code below is just an example of how INPUT TYPE="FILE" could be used as an open file dialog box, I just dont know enough HTA coding to get you much farther. I modified it from the code in the link below. I just removed "xml" from the variable names and changed the start folder / file mask to C:\*.*.

I would think your additional code could go in the GetFile_onchange event. I don't know that there are many other options (other than installing 3rd party software or a windows xp dll.... which I did see mentioned in some google hits). I would be interested in finding a better way for Win7 also.

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/0d0c3e4b-ae6f-4f05-ab6a-140e72027c14

CODE

<head> <HTA:APPLICATION SCROLL="yes" SINGLEINSTANCE="yes" > </head> <script language="VBScript"> Sub Window_onload ' Preload starting folder location and file mask GetFile.focus createobject("wscript.shell").sendkeys "C:\*.*{enter}" ' GetFile.click ' optional end sub Sub GetFile_onchange FileBox.value = GetFile.value End Sub </script> <body bgcolor="WhiteSmoke"> <br> <div> <label style='width:8%'>Get File: </label> <input type=file size=40 name=GetFile title="Press Browse to select a file"><br> <label style='width:8%'>File: </label> <input type="text" size=40 value="" name="FileBox"> </div> <br> </body>
frumpus (Programmer)
5 Jun 12 10:35
Actually, I was able to do what I wanted with MS Word objects. It requires that Word be installed, but it turned out to be really simple and works exactly the way I want it to. This will at least buy me enough time to learn some HTA which looks pretty awesome.

Thanks guys for your efforts in this thread!

CODE

Const MY_DOCUMENTS = &H5& Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(MY_DOCUMENTS) Set objFolderItem = objFolder.Self strPath = objFolderItem.Path Const msoFileDialogOpen = 1 Set objWord = CreateObject("Word.Application") objWord.ChangeFileOpenDirectory(strPath) objWord.FileDialog(msoFileDialogOpen).Title = "Select a file:" objWord.FileDialog(msoFileDialogOpen).AllowMultiSelect = False 'only want 1 file If objWord.FileDialog(msoFileDialogOpen).Show = -1 Then objWord.WindowState = 2 For Each objFile in objWord.FileDialog(msoFileDialogOpen).SelectedItems strFilePath = objFile Next 'now i can open the file as a text stream and do whatever i want End If objWord.Quit

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close