VBA 中的文本文件:打开/查找替换/另存为/关闭文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10434335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text file in VBA: Open/Find Replace/SaveAs/Close File
提问by Russell Saari
Here is pseudocode for what I am hoping to do:
这是我希望做的伪代码:
Open text File
Find "XXXXX" and Replace with "YYYY"
Save text File As
Close text file
This is what I have so far
这是我到目前为止
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
But instead of overwriting the original text file, I want to "save as" to a new file.
但不是覆盖原始文本文件,我想“另存为”到一个新文件。
采纳答案by Jean-Fran?ois Corbett
Just add this line
只需添加这一行
sFileName = "C:\someotherfilelocation"
right before this line
就在这条线之前
Open sFileName For Output As iFileNum
The idea is to open and write to a differentfile than the one you read earlier (C:\filelocation
).
这个想法是打开并写入与您之前阅读的文件不同的文件 ( C:\filelocation
)。
If you want to get fancy and show a real "Save As" dialog box, you could do this instead:
如果你想变得有趣并显示一个真正的“另存为”对话框,你可以这样做:
sFileName = Application.GetSaveAsFilename()
回答by Steve Rindsberg
Why involve Notepad?
为什么涉及记事本?
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
回答by Bojie Shuai
Guess I'm too late...
估计我来晚了……
Came across the same problem today; here is my solution using FileSystemObject
:
今天遇到了同样的问题;这是我使用的解决方案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
回答by zmx
I have had the same problem and came acrosse this site.
我遇到了同样的问题,并遇到了这个网站。
the solution to just set another "filename" in the
在
... for output as... command was very simple and useful.
...输出为... 命令非常简单和有用。
in addition (beyond the Application.GetSaveAsFilename() Dialog)
此外(除了 Application.GetSaveAsFilename() 对话框之外)
it is very simple to set a** new filename** just using
只需使用即可设置**新文件名**非常简单
the replacecommand, so you may change the filename/extension
在替换命令,这样你就可以改变文件名/扩展
eg. (as from the first post)
例如。(从第一篇文章开始)
sFileName = "C:\filelocation"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
content = (...edit the content)
Close iFileNum
now just set:
现在只需设置:
newFilename = replace(sFilename, ".txt", ".csv") to change the extension
newFilename = replace(sFilename, ".txt", ".csv") 更改扩展名
or
或者
newFilename = replace(sFilename, ".", "_edit.") for a differrent filename
newFilename = replace(sFilename, ".", "_edit.") 不同的文件名
and then just as before
然后就像以前一样
iFileNum = FreeFile
Open newFileName For Output As iFileNum
Print #iFileNum, content
Close iFileNum
I surfed over an hour to find out how to rename a txt-file,
我浏览了一个多小时以了解如何重命名 txt 文件,
with many different solutions, but it could be sooo easy :)
有许多不同的解决方案,但它可能非常简单:)
回答by satheesh kumar
This code will open and read lines of complete text file That variable "ReadedData" Holds the text line in memory
此代码将打开并读取完整文本文件的行变量“ReadedData”将文本行保存在内存中
Open "C:\satheesh\myfile\Hello.txt" For Input As #1
do until EOF(1)
Input #1, ReadedData
loop**