Search Tools Links Login

Browse Folder without API reference in Word, Excel in VBA

This is a sample code to Open Folder Browsing Windows in Word and Excel. user need not to include any special reference or API code for this purpose.

Original Author: Chandan Banga


Function returns the absolute path of the folder

Side Effects

no side effect


Function BrowseFolder(Optional OpenAt As Variant) As Variant
  Dim ShellApplication As Object
  Set ShellApplication = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
  On Error Resume Next
  BrowseFolder = ShellApplication.self.Path
  Set ShellApplication = Nothing
  Select Case Mid(BrowseFolder, 2, 1)
    Case Is = ":"
      If Left(BrowseFolder, 1) = ":" Then GoTo err1
    Case Is = ""
      If Not Left(BrowseFolder, 1) = "" Then GoTo err1
    Case Else
      GoTo err1
  End Select
Exit Function

  BrowseFolder = False
End Function

Sub OpenFolder()
  Dim Response As Variant
  Response = BrowseFolder
  If Response = False Then
    MsgBox "You have selected Invalid Folder", vbInformation
    MsgBox "Selected Folder - " & Response, vbInformation
  End If
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 108 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.