這裏是我所希望做的僞代碼:文本文件:打開/查找替換/另存爲/關閉文件
Open text File
Find "XXXXX" and Replace with "YYYY"
Save text File As
Close text file
這是我迄今爲止
Private Sub CommandButton1_Click()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
' Edit as needed
sFileName = "C:\filelocation"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "DIM A", "1.75")
sTemp = Replace(sTemp, "DIM B", "2.00")
sTemp = Replace(sTemp, "DIM C", "3.00")
sTemp = Replace(sTemp, "DIM D", "4.00")
'Save txt file as (if possible)
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
'Close Userform
Unload UserForm1
End Sub
但而不是覆蓋原來的文本文件,我想「另存爲」一個新的文件。
有反正我可以做一個另存爲在此代碼? –
此代碼與問題中的代碼有什麼不同? –
現在,OP已將其複製/粘貼到原始問題的*編輯版*中,完全沒有。 ;-)感謝您添加另存爲(以不同的文件名)修復。 –