Convert this code to have Pop Up folder selection

HotLanta

Board Regular
Joined
Nov 3, 2005
Messages
156
Hi all,

I have merged two pieces of code together and it is working perfectly. To make it more perfect I wanted to modify it once more but I am having trouble making that work.
I have some code that looks for text files in a hard coded folder location. Then a pop up box asks the user what text they want to change in the files and then another asks what they want to change it to.

I would like for instead of being hard coded to a particular folder location, a pop up ask the user to select the file location.

In the code below I simply typed "Process" in the hard coded folder location so not as to put any real information out there....

Sub Replace_text()

Dim objFSO, StrFolder, objFolder, objFile
Dim x As String, y As String
Dim strOldValue, strNewValue, objRead, strContents, objWrite

Const ForReading = 1
Const ForWriting = 2

StrFolder = "Process"

x = InputBox("Replace What?", "Replace What")
y = InputBox("Replace With?", "Replace With")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(StrFolder)
For Each objFile In objFolder.Files
Set objRead = objFSo_Opentextfile(objFile.Path, ForReading)

On Error Resume Next

strContents = objRead.readall
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Cannot read: " & objFile.Path
strContents = ""
End If
On Error GoTo 0
objRead.Close
If (InStr(strContents, x) > 0) Then
strContents = Replace(strContents, x, y)
Set objWrite = objFSo_Opentextfile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
Next

End Sub

Thanks
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,431
Replace the StrFolder = line with:
Code:
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select the folder"
        If .Show Then
            StrFolder = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
 

Watch MrExcel Video

Forum statistics

Threads
1,109,493
Messages
5,529,187
Members
409,856
Latest member
MAO
Top