Advertisement
ASP_Volume3 Microsoft Office Apps/VBA #63864

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.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
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

err1:
  BrowseFolder = False
End Function

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

Original Comments (3)
Recovered from Wayback Machine