Browse the file and directory from cell contents

greegan

Well-known Member
Joined
Nov 18, 2009
Messages
643
Office Version
  1. 365
Platform
  1. Windows
I have someone else's work here to demonstrate what I am trying to do.

Basically I'm going to run a routine on files specified by the directory and file identified in a cell:
ie in cell A1 it may appear as
Code:
c\user\project folder\filename1
(not sure if i used the tags correctly)
next to cell A will be a browse button so I can select the above file.
The string will be called in a later code.
Right now, I only have a function and code allowing me to select the directory.
I know how to bring up the dialog box to open the file. However with the MajorDomo macro I am writing to reference Cell A1, it will also call other files in a similar fashion in cells A3, A5, and A7.

here's the code I have. I need to SELECT the file and directory, not open it.

Thank you.

Code:
Declare Function SHBrowseForFolder Lib "shell32.dll" _Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Function GetDirectory(Optional Msg) As String
    Dim bInfo As BROWSEINFO
    Dim Path As String
    Dim r As Long, x As Long, pos As Integer
 
'   Root folder = Desktop
    bInfo.pidlRoot = 0&


'   Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
    End If
    
'   Type of directory to return
    bInfo.ulFlags = &H1


'   Display the dialog
    x = SHBrowseForFolder(bInfo)
    
'   Parse the result
    Path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal Path)
    If r Then
        pos = InStr(Path, Chr$(0))
        GetDirectory = Left(Path, pos - 1)
    Else
        GetDirectory = ""
    End If
End Function














'Set variables and constants
Dim MyPath As String
Dim DirName As String, NextFile As String, ChangeTo As String
Dim ErrorMsg As String
Dim Path As String, Msg As String
Dim RowCounter As Integer
Dim Unchanged As Integer
Const RenamedColour As Integer = 36
Const ProblemColour As Integer = 40
Const UnchangedColour As Integer = 35


Sub GetSourcePath()
'    ActiveSheet.Unprotect
'Get the path using shell32.dll routine
    Msg = "Select a directory for the file list"
    Path = GetDirectory(Msg)
    If Path <> "" Then Range("Path").Value = Path
'    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, AllowFormattingCells:=True, AllowInsertingRows:=True, AllowDeletingRows:=True, AllowSorting:=True
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,223,739
Messages
6,174,217
Members
452,551
Latest member
croud

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