VBA-FileToFileUpdate

Posted on 2020-02-26 22:40  wzhw2015  阅读(153)  评论(0编辑  收藏  举报

Public Sub FileToFileUpdate(ByVal fileName As String, ByVal strFrm As String, ByVal strTo As String)

Dim newFileName As String

newFileName = fileName & "_bak"

Name fileName As newFileName

Dim vFso As New Scripting.FileSystemObject
Dim vTextStreamR As TextStream
Dim vTextStreamW As TextStream

Set vTextStreamR = vFso.GetFile(newFileName).OpenAsTextStream(ForReading, TristateUseDefault)

Call vFso.CreateTextFile(fileName, True)

Set vTextStreamW = vFso.GetFile(fileName).OpenAsTextStream(ForWriting, TristateUseDefault)

Do Until vTextStreamR.AtEndOfStream

vTextStreamW.WriteLine Replace(vTextStreamR.ReadLine, strFrm, strTo)

Loop


Set vTextStreamR = Nothing
Set vTextStreamW = Nothing

Call vFso.DeleteFile(newFileName)
Set vFso = Nothing

End Sub

Copyright © 2024 wzhw2015
Powered by .NET 9.0 on Kubernetes