Excel宏可以在任何文本文件中查找和替换多个字符串

时间:2016-05-18 06:09:09

标签: excel vba excel-vba

我对脚本很天真,但我使用您网站的下面代码来替换文本文件中的字符串,它运行正常。但是,我不想指定文件名,它应该准备任何文件,如(* .txt或* .xml)。所以,如果有人能帮忙,我感激不尽。

由于

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

2 个答案:

答案 0 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
</head>
<body>
<input type="file" id="imgUpload">
  <div id="myBackground">
  <img src="#" alt="your image" />
  </div>
</body>
</html>

答案 1 :(得分:0)

您可以遍历数组中的项目!!

val foo = fn : string -> int -> unit
val bar = fn : string -> int -> unit
- bar "toomanyfiles.sml" 1024;
val it = () : unit
- foo "toomanyfiles.sml" 1024;

uncaught exception Io [Io: openIn failed on "toomanyfiles.sml", Too many open files]
  raised at: Basis/Implementation/IO/text-io-fn.sml:783.25-783.71