VBA Create Object Shell Application Problem

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
I have the code shown below that someone suggested to me, but I need to change it so that a specific directory is given as starting point for users to then browse for folders to be placed in a user form listbox.
I have no idea what to change to make this happen. Any advise would be appreciated.
The starting point will always be: T:\\Finance\SohoMaccs\2018maccs\Monthly Reports\

Code:
Private Sub UserForm_Initialize()
    Dim oFolder As Object, oSubFolder As Object
    Dim sFoldersList As String
    
    Const SF_DOCUMENTS As Long = 5


    sFolder = CreateObject("Shell.Application").Namespace(CVar(SF_DOCUMENTS)).Self.Path


    With CreateObject("Scripting.FileSystemObject")
        
        Set oFolder = .GetFolder(sFolder)
        
        On Error GoTo NextSubFolder '   Error 70 = Permission Denied
        If oFolder.Subfolders.Count > 0 Then
            For Each oSubFolder In oFolder.Subfolders
                sFoldersList = sFoldersList & oSubFolder.Path & ";"
NextSubFolder:
            Next
        End If
    End With


    sFoldersList = Left(sFoldersList, Len(sFoldersList) - 1)


    Me.lbFolder.List = Split(sFoldersList, ";")


    Me.Frame2.Visible = False
    Me.Frame3.Visible = False




End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi drluke,

Try this from code I've adapted from abousetta's thread number 5 from here which I was also a part of:

Code:
Option Explicit
Private Sub UserForm_Initialize()

    ListFolders "T:\Finance\SohoMaccs\2018maccs\Monthly Reports\", True '<< Change to your needs. True will include subfolders, False will only return the initial path.

End Sub
Sub ListFolders(SourceFolderName As String, IncludeSubfolders As Boolean)
    
    Dim objFSO As Object
    Dim objSourceFolder As Object
    Dim objSubFolder As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objSourceFolder = objFSO.GetFolder(SourceFolderName)

    If IncludeSubfolders = True Then
        For Each objSubFolder In objSourceFolder.SubFolders
            ListFolders objSubFolder.Path, True
            Me.lbFolder.AddItem objSubFolder.Path
        Next objSubFolder
    Else
        Me.lbFolder.AddItem objSourceFolder
    End If
    
    Set objSubFolder = Nothing
    Set objSourceFolder = Nothing
    Set objFSO = Nothing
    
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top