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 = objFSpentextfile(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 = objFSpentextfile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
Next
End Sub
Thanks
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 = objFSpentextfile(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 = objFSpentextfile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
Next
End Sub
Thanks