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

need to open a file dialog box, select a file (text file) and display thecontent.

Status
Not open for further replies.

dabiri

Technical User
Dec 8, 2014
33
US
hi.
I have an hta/vbs app that will present a screen. have user click a start button and it should present a dialog box of file in a specific folder. the user will select the file, click the dialog box ok button and it will display the content of the file.
here's what I found in tek-tips vbs:
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
but this just opens the dialog box, selects the file and when I click the ok button, it errors out.
thanks.
 
thanks guitarzan.
my application is hta/vbs
the file dialog comes up ok.
when I click on the file name, nothing happens.
I like the file to be opened and show the content, which is a text file.
thanks.
 
In the sample I linked to, the variable "file" would contain the full path to the file selected. You should be able to do something like this to open the file in Notepad

Code:
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "notepad.exe " & file
Set oShell = Nothing
 
thanks so much.
I will try it.
best regards
 
Guitarzan,
I used the code that you posted at the end of the other one.
the variable "file" has null value - I displayed it before notepad, it's null.
notepad opens empty.
 
it comes back with "no object selected - cancel selected
 
thanks Guitarzan.
here's the complete solution that works - tested:
Code:
'set the type of dialog box you want to use
'1 = Open
'2 = SaveAs
'3 = File Picker
'4 = Folder Picker
Const msoFileDialogOpen = 1

msgbox "started"

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

'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
strInitialPath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"

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

With objWord.FileDialog(msoFileDialogOpen)
   'set the window title to whatever you want
   .Title = "Select the file to process"
   'I changed this to false because I'm working with one file at a time
   .AllowMultiSelect = False
   'Get rid of any existing filters
   .Filters.Clear
   '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
   .Filters.Add "All Files", "*.*"
   .Filters.Add "Excel Files", "*.xls;*.xlsx"
   .Filters.Add "Text Files", "*.txt"
   .Filters.Add "Various Files", "*.xls;*.doc;*.vbs"
         
   '-1 = Open the file
   ' 0 = Cancel the dialog box
   '-2 = Close the dialog box
   'If objWord.FileDialog(msoFileDialogOpen).Show = -1 Then  'long form
   If .Show = -1 Then  'short form
      'Set how you want the dialog window to appear
      'it doesn't appear to do anything so it's commented out for now
      '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
      For Each File in .SelectedItems  'short form
         'Change the Word dialog object to a file object for easier manipulation
         Set objFile = fso.GetFile(File)
         'Display the full path to the file
         msgbox objFile.Path
         'Display the path to the folder that the file is in
         msgbox objFile.ParentFolder
         'Display just the name of the file
         msgbox objFile.Name
      Next    
   Else 
   End If
End With 

'Close Word
objWord.Quit

Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "notepad.exe " & objFile.Path  


    Set oShell = Nothing
    Set oItem = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top