Browse for Files in HTA

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
jwestan
Posts: 48
Joined: Wed Mar 18, 2009 2:48 am

Browse for Files in HTA

Post by jwestan » Fri Apr 24, 2009 2:05 am

I am trying to get the BrowserForFolders method to be used to select a file, actually a word doc. I found a vbscipt that uses the BrowseForFolders to be able to select folders or files.
I am getting a syntax error and I dont know what it is as it looks fine to me.
Can I get someone to look at it for me or perhaps suggest a better method?

Thanks

Code: Select all

<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">  
<title>Browse for Files Test</title>  
<script language="vbscript">  
Sub ChooseSaveFolder     
Dim selectFile  
 ' Flags for the options parameter  
Const BIF_returnonlyfsdirs   = &H0001  
Const BIF_dontgobelowdomain  = &H0002  
Const BIF_statustext         = &H0004  
Const BIF_returnfsancestors  = &H0008  
Const BIF_editbox            = &H0010  
Const BIF_validate           = &H0020  
Const BIF_browseforcomputer  = &H1000  
Const BIF_browseforprinter   = &H2000  
Const BIF_browseincludefiles = &H4000  
selectFile = PickFolder( _  
       "Select a file. " & VbCrLf & _  
    "Current starting folder path: ", BIF_returnonlyfsdirs + BIF_browseincludefiles, "")  
If selectFile = "-5" Then  
 MsgBox "Not possible to select files in root folder",48, "Root Folder Select Error Message"  
Else  
 If selectFile = "-1" Then  
  MsgBox "No file selected; Cancel Clicked"  
 Else  
  txtFile.value = selectFile  
 End If  
End If   
End Sub   
Function PickFolder(title, flag, dir)  
Dim SA, F, tmp  
On Error Resume Next   
Set SA = CreateObject("Shell.Application")  
Set F = SA.BrowseForFolder(&H0, title, flag, dir )  
If Err.Number <> 0 Then  
 If Err.Number = 5 Then   
  BrowseForFolder = "-5"  
  Err.Clear  
  Set SA = Nothing  
  Set F = Nothing  
  Exit Function  
 End If  
End If   
' Now we try to retrieve the full path.  
BrowseForFolder = F.ParentFolder.ParseName(F.Title).Path  
'Handling: Cancel button and selecting a drive  
If Err <> 0 Then  
 If Err.Number = 424 Then 'Handle Cancel Button  
  BrowseForFolder = "-1"  
 Else  
  Err.Clear  
        ' Handle situation in which user selects a drive.  
        ' Extract drive letter from the title--first search  
        ' for a colon (:).  
        tmp = InStr(1, F.Title, ":")  
         If tmp > 0 Then ' A : is found; use two characters and add .  
    BrowseForFolder = Mid(F.Title, (tmp - 1), 2,) & ""  
   End If    
 End If  
End If   
'If (Not F Is Nothing) Then  
 ' PickFolder = F.Items.Item.path  
'End If  
Set F = Nothing  
Set SA = Nothing  
End Function  
</script>  
<hta:application  
 applicationname="MyHTA"   
 border="dialog"  
 borderstyle="normal"  
 caption="My HTML Application"  
 contextmenu="no"  
 icon="myicon.ico"  
 maximizebutton="no"  
 minimizebutton="yes"  
 navigable="no"  
 scroll="no"  
 selection="no"  
 showintaskbar="yes"  
 singleinstance="yes"  
 sysmenu="yes"  
 version="1.0"  
 windowstate="normal"  
>  
</head>  
<body>  
<table border="1" cellpadding="0" width="533">  
      <tr>  
       <td><input type="Button" value="Step 1. Browse to File..." onclick="ChooseSaveFolder"></td>  
       <td align="right"><input name="TxtFile"></td>  
      </tr>  
</table>  
</body>  
</html>

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Browse for Files in HTA

Post by jvierra » Fri Apr 24, 2009 2:19 am

Run as VBScript file and remove ALL On Error statements. You will see the errrors reported. I see one but I think there are more.


Once it works as a VBS file you can then move it into HTA.


User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Browse for Files in HTA

Post by jvierra » Fri Apr 24, 2009 3:03 am

Note that the ChooseFolder flag logic does not work teh way the code thinks it does. It will never alert to picking a root folder on XP and later with those flags.

Check the MSDN docs for correct usage of the shell control.

You also have assignment errors in HTML.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Browse for Files in HTA

Post by jvierra » Fri Apr 24, 2009 3:28 am

Here is the way the function needs to be called in order to get the filter to work correctly.

Code: Select all

	
Function PickFolder(title, flag, dir)
	
    Dim SA, F, tmp
	
    Set SA = CreateObject("Shell.Application")
    Set F = SA.BrowseForFolder(&H0, title, flag, dir )
    On Error Resume Next 
    PickFolder = F.ParentFolder.ParseName(F.Title).Path
    If Err.Number  0 Then
            PickFolder = CStr(Err.Number)
            Exit Function
    End If
    
End Function
	

Locked