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

Odd error using BrowseForFolder

Status
Not open for further replies.

frumpus

Programmer
Aug 1, 2005
113
US
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.
 
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
 
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.
 
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
 
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
 
@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. :-/

 
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.
 
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 :-/

 
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.

[URL unfurl="true"]http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/0d0c3e4b-ae6f-4f05-ab6a-140e72027c14[/url]

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>
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top