English 中文(简体)
VBA中文本文档:露天/完整替代文件
原标题:Text file in VBA: Open/Find Replace/SaveAs/Close File

这里是我所希望做的假装:

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

但是,我不想重写原始文本档案,而是想把新档案“空洞”。

最佳回答

仅增加这一条。

sFileName = "C:someotherfilelocation"

权利主张

Open sFileName For Output As iFileNum

想法是,在一份different <>/em>文档上打开并写上比你早先读到的文档(C:file <<>>>>>。

If you want to get fancy and show a real "Save As" dialog box, you could do this instead:

sFileName = Application.GetSaveAsFilename()
问题回答

Guess I m太晚......

今天,在同一个问题上,我的解决办法是使用:

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	est.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

为什么需要“灯塔”?

Sub ReplaceStringInFile()

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

  Edit as needed
sFileName = "C:Temp	est.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

我也存在同样的问题,并跨越了这个地点。

公正确定另一个“姓名”的解决办法

页: 1

此外(超出申请范围)。 GetSaveAsFilename(迪亚洛)

it is very simple to set a** new filename** just using

the replace command, so you may change the filename/extension

例如。 (从第一个员额起)

sFileName = "C:filelocation"
iFileNum = FreeFile

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

Close iFileNum

仅限:

新Filename = 替换(sFilename, “.txt”,“csv”,改为

newFilename = replace(sFilename, ".", "_edit.") f或 a differrent filename

and then just as bef或e

iFileNum = FreeFile
Open newFileName F或 Output As iFileNum

Print #iFileNum, content
Close iFileNum 

我超过一小时,以找出如何改用一台拖轮机,

具有许多不同的解决办法,但可以很容易:

This code will open and read lines of complete text file That variable "ReadedData" Holds the text line in memory

Open "C:satheeshmyfileHello.txt" For Input As #1

do until EOF(1)   

       Input #1, ReadedData
loop**




相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...