2012-05-03 29 views
5

這裏是我所希望做的僞代碼:文本文件:打開/查找替換/另存爲/關閉文件

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 

但而不是覆蓋原來的文本文件,我想「另存爲」一個新的文件。

回答

5

此行

Open sFileName For Output As iFileNum 

的想法是打開和寫入不同文件比你早看一個前右剛加入這一行

sFileName = "C:\someotherfilelocation" 

C:\filelocation )。

如果你想獲得幻想,並顯示一個真正的「另存爲」對話框中,你可以這樣做,而不是:

sFileName = Application.GetSaveAsFilename() 
8

爲什麼涉及記事本?

Sub ReplaceStringInFile() 

Dim sBuf As String 
Dim sTemp As String 
Dim iFileNum As Integer 
Dim sFileName As String 

' Edit as needed 
sFileName = "C:\Temp\test.txt" 

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, "THIS", "THAT") 

iFileNum = FreeFile 
Open sFileName For Output As iFileNum 
Print #iFileNum, sTemp 
Close iFileNum 

End Sub 
+0

有反正我可以做一個另存爲在此代碼? –

+0

此代碼與問題中的代碼有什麼不同? –

+2

現在,OP已將其複製/粘貼到原始問題的*編輯版*中,完全沒有。 ;-)感謝您添加另存爲(以不同的文件名)修復。 –

-1

此代碼將打開並閱讀該變量「ReadedData完整的文本文件 行「在內存中保存的文本行

Open "C:\satheesh\myfile\Hello.txt" For Input As #1 

do until EOF(1) 

     Input #1, ReadedData 
loop** 
+0

這並沒有解決這個問題。 – Smandoli

3

想我已經太晚了......

跨越今天同樣的問題就來了;這裏是一個使用FileSystemObject我的解決方案:

Dim objFSO 
Const ForReading = 1 
Const ForWriting = 2 
Dim objTS 'define a TextStream object 
Dim strContents As String 
Dim fileSpec As String 

fileSpec = "C:\Temp\test.txt" 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTS = objFSO.OpenTextFile(fileSpec, ForReading) 
strContents = objTS.ReadAll 
strContents = Replace(strContents, "XXXXX", "YYYY") 
objTS.Close 

Set objTS = objFSO.OpenTextFile(fileSpec, ForWriting) 
objTS.Write strContents 
objTS.Close 
0

我有同樣的問題,來到acrosse這個網站。

解決剛纔設置另一個「文件名」中的

... 輸出...命令非常簡單實用的。

除了(超出Application.GetSaveAsFilename()對話框)

它是設置** 只是用

一個**新的文件名很簡單的更換命令, 所以你可能更改文件名/擴展名

例如。 (從第一柱)

sFileName = "C:\filelocation" 
iFileNum = FreeFile 

Open sFileName For Input As iFileNum 
content = (...edit the content) 

Close iFileNum 

現在只需設置:

newFilename =取代(sFilename,名爲 「.txt」,」。CSV 「)改變擴展

newFilename =取代(sFilename,」。「, 」_edit。「)用於differrent文件名

,然後就像

之前
iFileNum = FreeFile 
Open newFileName For Output As iFileNum 

Print #iFileNum, content 
Close iFileNum 

我上網了一個多小時,瞭解如何重新命名一個txt文件,

與許多不同的解決方案,但它可以很容易:)

相關問題