아래 코드는 텍스트 파일의 단어 ( "This")를 다른 단어 ( "That")로 바꾸는 작업입니다. 마찬가지로 동일한 텍스트 파일에서 하나 이상의 단어를 대체해야합니다.
내 요구 사항은 "This"를 "That"로 바꾸고 "From"을 "TO"로 바꾸는 것입니다. 그에 따라 아래 코드를 수정할 수 있습니까?
Sub ReplaceStringInFile()
Dim objFSO As Object
Dim objFil As Object
Dim objFil2 As Object
Dim StrFileName As String
Dim StrFolder As String
Dim SstrAll As String
Set objFSO = CreateObject("scripting.filesystemobject")
StrFolder = "c:\macro\"
StrFileName = Dir(StrFolder & "*.txt")
Do While StrFileName <> vbNullString
Set objFil = objFSO.opentextfile(StrFolder & StrFileName)
strAll = objFil.readall
objFil.Close
Set objFil2 = objFSO.createtextfile(StrFolder & StrFileName)
objFil2.Write Replace(strAll, "THIS", "THAT")
objFil2.Close
StrFileName = Dir
Loop
End Sub
이 줄은 "this"를 "that"으로 바꿉니다.
objFil2.Write Replace(strAll, "THIS", "THAT")
그 아래에 다음 줄을 추가하십시오.
objFil2.Write Replace(strAll, "From", "To")
편집하다:
죄송합니다. 코드를 테스트하지 않았습니다. 대신 이것을 시도하십시오.
Sub ReplaceStringInFile()
Dim objFSO As Object, objFil As Object, objFil2 As Object
Dim StrFileName As String, StrFolder As String, strAll As String, newFileText As String
Set objFSO = CreateObject("scripting.filesystemobject")
StrFolder = "c:\macro\"
StrFileName = Dir(StrFolder & "*.txt")
Do While StrFileName <> vbNullString
Set objFil = objFSO.opentextfile(StrFolder & StrFileName)
strAll = objFil.readall
objFil.Close
Set objFil2 = objFSO.createtextfile(StrFolder & StrFileName)
'change this to that in text
newFileText = Replace(strAll, "THIS", "THAT")
'change from to to in text
newFileText = Replace(newFileText, "from", "to")
'write file with new text
objFil2.Write newFileText
objFil2.Close
StrFileName = Dir
Loop
End Sub
작동하는지 알려주세요.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다