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

Open File (Windows Server 2003) 1

Status
Not open for further replies.

Skie

Programmer
Jun 21, 2004
475
US
The following code works great for WinXP, and it's been what I was using to present the user an Open File dialog window.
Code:
Set oDialog = CreateObject("UserAccounts.CommonDialog")
oDialog.Filter = "Sessions|*.edp|508 Sessions|*508.edp"
oDialog.InitialDir = "C:\Sessions\"
iResult = oDialog.ShowOpen
If iResult = -1 Then
  WScript.Echo oDialog.FileName
End If
But, moving to Windows Server 2003 prevents me from being able to use that object. I was able to find this Open File dialog window, but it's not very useful because I can't filter or set the directory.
Code:
Set oFODlg = CreateObject("SAFRCFileDlg.FileOpen")
iResult = oFODlg.OpenFileOpenDlg
If iResult = 1 Then
  WScript.Echo oFODlg.FileName
End If
Similarly, I know it's possible with an .htm file to have a browse button with a starting directory (this doesn't seem to work in HTA however)... The problem I have here, is that I don't know if/how to apply a filter. Is it possible?
Code:
<input type="file" name="myFile" value="C:\Sessions\">
What I'm hoping for is someone knows another option that I've missed or how to make what I have work. If nothing else, I can code my own open file dialog box.

Thanks
 
I think you may just have to code your own dialog box. I don't think I've seen a filter using the later of the two methods mentioned like you can using UserAccount.CommonDialog . You could try something like this, but this would simply be a check after you select a file...not like the filter you get with UserAccount.CommonDialog.

Code:
<html>
<head>
<title>Browse For File</title>
<script language="vbscript">
   Sub CheckFileExtension
'         On Error Resume Next
      
       Dim objDict     :    Set objDict = CreateObject("Scripting.Dictionary")
       Dim strFilePath    :    strFilePath = window.document.getElementById("filepath").value
      
       ' define allowable extensions
       objDict.Add "csv", ""
       objDict.Add "txt", ""
      
       If Not objDict.Exists(Right(strFilePath, 3)) Then
           alert "This is not a valid file!"
       Else
           alert "Valid File...."
       End If
   End Sub
</script>
</head>
<body bgcolor="#272936" style="overflow:auto;color:#FFFFFF;">
<input type="file" id="filepath">
<br><br>
<input type="button" value="Some Button" onclick="CheckFileExtension">
</body>
</html>

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
The other option requires a developer license for VB5, VB6, or possibly other products of that vintage. VB.Net and later Visual Studio licenses may include what you need as well, though most likely not the Express Editions.

VB5CCE is (was) free if you can locate a copy of it. It may provide the proper license once installed.

You can use MakeCab to create a packaged Common Dialog control (or download the packaged CAB file from Microsoft) and then use LPKTool to create an IE License Package (LPK) file. Then you can refer to these two files using an <OBJECT> tag for the Common Dialog control, and another <OBJECT> tag to instantiate the IE License Package Manager. A <PARAM> tag within this latter <OBJECT> must reference the LPK file.

Using ActiveX Controls to Automate Your Web Pages and see the section "Run-time licensing."

This gives you two extra files to distribute with your application, but it does not require component registration or a formal installer package to be run. I use this in HTAs quite frequently.
 
Code:
<html>
<head>
<title>Open File</title>
<HTA:APPLICATION
  applicationName = "Open_File"
  version = "1.01.0001"
  id = "oOpen_File"
  singleInstance = "yes"
  border = "thin"
  maximizeButton = "no"
/>
<script type="text/vbscript" language="vbscript">
window.resizeTo 245, 258
window.moveTo 0, 0
Sub window_onLoad()
  sPath = "C:\"
  sArgs = oOpen_File.commandLine
  aArgs = Split(sArgs," /")
  For Each sArg In aArgs
    If Len(sArg) > 2 Then
      sArg = UCase(sArg)
      Select Case Left(sArg,1)
      Case "P"
         sPath = Right(sArg,Len(sArg)-2)
      Case "E"
         sExt = Right(sArg,Len(sArg)-2)
      End Select
    End If
  Next
  GetFiles sPath, sExt
End Sub

Sub GetFiles(sPath, sExt)
  sExt = LCase(sExt)
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  If oFSO.FolderExists(sPath) Then
    Set oFolder = oFSO.GetFolder(sPath)
    For Each oFile In oFolder.Files
      sName = LCase(oFile.Name)
      If sExt = "" Then
        Set oOption = document.createElement("option")
        oOption.text = oFile.Name
        oOption.value = sPath
        oFiles.add(oOption)
      ElseIf Right(sName,Len(sExt)) = sExt Then
        Set oOption = document.createElement("option")
        oOption.text = oFile.Name
        oOption.value = sPath
        oFiles.add(oOption)
      End If
    Next
  Else
    MsgBox "Folder Not Found",0,"Error"
  End If
End Sub

Sub oCancel_onClick
  window.close
End Sub

Sub oOpen_onClick
  For Each oOption In oFiles.options
    If oOption.Selected Then
      sFile = oOption.value & oOption.text
      MsgBox sFile
    End If
  Next
End Sub

</script>
<style type="text/css">
 body       { margin: 2px;
              padding: 0;
              background: #000; }
#oWrapper   { width:214px;
              background: #FFFFDD;
              padding: 7px;}
#oFiles     { font: 12pt Arial, Serif;
              width:200px;}
.aButton    { font: bold 10pt Verdana, Sans-Serif;
              width:98px;}
</style>
</head>
<body>
  <div id="oWrapper">
    <select id="oFiles" size="10"></select><br/>
    <input type="button" class="aButton" value="Open" id="oOpen">
    <input type="button" class="aButton" value="Cancel" id="oCancel">
  </div>
</body>
</html>

The code above, while a bit simple, meets my needs of a filter and a specific path. It works so long as I always want the user to see an HTA window. I can just add code to the HTA to handle whatever I want to do with the file.

But, if I call it from vbscript, the only method I know to get the value is to use a WshShell.Run and have the HTA write to a text file for the path and filename then use the vbscript to read that value. Is there a way to have the HTA return the path and filename directly?
 
I haven't seen an HTA returning a value, but I have seen it with an IE window. Check out some of the examples Tom Lavedas has posted: An example would be the third script he as listed. It is not a file path, but it is still retrieving information and passing it back to the .vbs.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top