SoFunction
Updated on 2025-04-07

3 ways to browse local files using VBS to get the full path

Because of the differences in system components, the code has different codes.

XP:

Function BrowseForFile( )
' Select File dialog based on a script by Mayayana
' Known issues:
' * Tree view always opens Desktop folder
' * In Win7/IE8 only the file NAME is returned correctly, the path returned will always be C:\fakepath\
' * If a shortcut to a file is selected, the name of that FILE will be returned, not the shortcut's
'On Error Resume Next 
'===========1, file browserer in XP ==================
Set objDialog = CreateObject("")
 = "txt|*.txt"
 = "C:\"
intResult = 
If intResult <> 0 Then
	BrowseForFile = 
	exit function
else
	msgbox "Error."
End If
end function
BrowseForFile

2003'for windows 2003

Function SelectAFile
Set objDialog = CreateObject("")
intResult = 
SelectAFile = 
End Function

Browser method:

Function BrowseForFile( )
 '===========2, fileselect in IE ====================== 
 'Another way to get file path
  Dim objIE, strSelected
  BrowseForFile = ""
  Set objIE = CreateObject( "" )
 = False
 = False
 = False 
 = 300
 = 100
   = True
  ( "about:blank" )
  Do Until  = 4
  Loop
 ' Center the dialog window on the screen
 With 
  = (.AvailWidth -  ) \ 4
  = (.Availheight - ) \ 4
 End With 
   "<HTML><BODY><INPUT ID=""FileSelect"" NAME=""FileSelect"" TYPE=""file""><BODY></HTML>"
  With 
    .focus
    .click
    strSelected = .value
  End With
  
  Set objIE = Nothing  
 If Trim(strSelected) = "" Then
 Msgbox "You selected no file."
 
 End If
 BrowseForFile = strSelected
End Function

BrowseForFile

Have you found that the above code is not easy to run? Here I will share with you an available one. Although the above code cannot be used, it is still good to learn and refer to it. Now, because the platform is willing to do so, it turns out that these codes can be used.

Function ChooseFile()
   Dim Result
   Result = ""
   Dim IE : Set IE = CreateObject("")
   With IE
     .Visible = False
     .Navigate("about:blank")
     Do Until .ReadyState = 4 : Loop
     With .Document
       .Write "<html><body><input id='f' type='file'></body></html>"
       With .
         .Focus
         .Click
         Result = .Value
       End With
     End With
     .Quit
   End With
   Set IE = Nothing
  ChooseFile = Result
End Function
ChooseFile

OK, this is all about the code on the function of selecting local files for vbs. Friends who need it can refer to it.