Good morning. I have the following code in an Excel worksheet. It's an ActiveX control button which runs the following when pressed..
Private Sub importSheetPathbtn_Click()
Dim fd As Office.FileDialog 'folder dialog box
Dim strPath As String 'holds the path/filename
Dim objFso 'create object to set as filesystem object in order to use folderexists... dumb
Set fd = Application.FileDialog(msoFileDialogFolderPicker) 'sets the filedialog variable as folder picker object
Set objFso = CreateObject("Scripting.FileSystemObject") 'creates the object
'set parameters
With fd
'checks to see if master path has content
If masterPathlbl.Caption <> "" Then
'if master path has content checks to see if the content points to an existing directory
If objFso.FolderExists(masterPathlbl.Caption) Then
.InitialFileName = masterPathlbl.Caption 'if master path points to an existing directory, opens folder dialog box in that directory
Else
.InitialFileName = "C:\" 'if master path content is not an existing directory then opens to C drive
End If
Else 'if master path has no content, opens to C drive
.InitialFileName = "C:\"
End If
If .Show = -1 Then 'if ok is pressed
strPath = .SelectedItems(1)
End If
End With
'if a valid path was seletced then places it into the master path
If objFso.FolderExists(strPath) Then
masterPathlbl.Caption = strPath 'places the selected folder into the master path label
End If
'clears objects
'fd = Nothing
'objFso = Nothing
End Sub
All it is doing is taking the folder the user selects and placing its path into a label as a caption. It works, but takes a good 10-15 seconds.
On a side note, I am also wondering how to properly close the objects that are created?
Any help would be appreciated, thanks for your time.