[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to clean all the txt in a txt file and then put a paragraph in it

Posted on 2005-04-07
7
Medium Priority
?
188 Views
Last Modified: 2012-05-05
Hi, Experts,

Is there any way to clean all the txt in the txt file firts and then put a paragraph in it.

Thanks,

Turbot
0
Comment
Question by:turbot_yu
6 Comments
 
LVL 54

Accepted Solution

by:
Ryan Chong earned 1000 total points
ID: 13724833
You can directly overwrite that file, like this:

Private Sub WriteFileText(ByVal FileName As String, ByVal Source As String, Optional noCrlf As Boolean = False, Optional Append As Boolean = False)
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    If Append Then
        Open FileName For Append As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    Else
        Open FileName For Output As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    End If
    Exit Sub
EHandler:
    'ShowErrMsg
    On Error Resume Next
    Close #Handle
End Sub

then try like:

myContent = Text1.Text
WriteFileText "C:\test.txt", myContent , True

get the idea?
0
 
LVL 1

Expert Comment

by:MarioLeo
ID: 13725903
What I always do is simply close the file, kill it and then create another file with the new content : 3 lines of code, hassle -free

Regards,

Mario
0
 
LVL 14

Assisted Solution

by:Shiju Sasidharan
Shiju Sasidharan earned 1000 total points
ID: 13726721
hi
or u can use FileSystemObjects
its easy
'Add reference
'Project Menu -> References -> Microsoft Scripting Runtime
'============================================

Private Sub Command1_Click()
    'Usage
        Call OverWriteFile("C:\MyFile.txt", "This is new data")
End Sub
Private Sub OverWriteFile(ByVal FileName As String, ByVal DataToBeWritten As String)
On Error GoTo Last
    Dim objFso As New FileSystemObject
    Dim objTextStream As TextStream
       
    Set objTextStream = objFso.OpenTextFile(FileName, ForWriting, True)
    objTextStream.Write DataToBeWritten
    objTextStream.Close
    Set objFso = Nothing
    MsgBox "File Updated", vbInformation
Last:
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Err.Clear
    End If
End Sub
'====================================

;-)
Shiju
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:ShawnCorbett
ID: 13729950
Instead of opening the file in append mode just open it in output mode and it will overwrite whatever is there.

open "Filename.txt" for output as #1
write #1, "paragraph"
close #1
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 14308751
Its been more than 21 days since a comment has been made. I am leaving this for cleanup.
Recommend: Split: ryancys , shijusn
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 14310221
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:

[Split: ryancys and shijusn]

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

egl1044
EE Cleanup Volunteer
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question