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

Posted on 2005-04-07
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.


Question by:turbot_yu
    LVL 48

    Accepted Solution

    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;
                    Print #Handle, Source
                End If
                On Error Resume Next
            Close #Handle
            Open FileName For Output As #Handle
                If noCrlf Then
                    Print #Handle, Source;
                    Print #Handle, Source
                End If
                On Error Resume Next
            Close #Handle
        End If
        Exit Sub
        On Error Resume Next
        Close #Handle
    End Sub

    then try like:

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

    get the idea?
    LVL 1

    Expert Comment

    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


    LVL 14

    Assisted Solution

    by:Shiju Sasidharan
    or u can use FileSystemObjects
    its easy
    'Add reference
    'Project Menu -> References -> Microsoft Scripting Runtime

    Private Sub Command1_Click()
            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
        Set objFso = Nothing
        MsgBox "File Updated", vbInformation
        If Err.Number <> 0 Then
            MsgBox Err.Description
        End If
    End Sub


    Expert Comment

    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
    LVL 29

    Expert Comment

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

    Expert Comment

    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.

    EE Cleanup Volunteer

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now