+ Reply to Thread
Results 1 to 6 of 6

Editing a text file from VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Editing a text file from VBA

    Hi,

    I'm looking to clear all lines in a text file from VBA, starting from the second line.

    From what I've seen so far it seems like the "fso" object is the way to go, but unfortunately I haven't found anyone doing what I'm trying to do exactly.

    Anyone able to help?

    Thanks!

    Mike

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Editing a text file from VBA

    Hi, Mike ,

    maybe you can start with a sample like this:
    Sub EF972540()
    Dim strPath As String
    Dim strText As String
    Dim intFile As Integer
    
    Const cstrSAMPLE As String = "Sample.txt"
    
    strPath = ThisWorkbook.Path & "\" & cstrSAMPLE
    If Dir(strPath) = "" Then
      MsgBox "Couldnīt find '" & cstrSAMPLE & "'!"
      End
    End If
    
    intFile = FreeFile
    Open strPath For Input As intFile
      Line Input #intFile, strText
    Close
    
    intFile = FreeFile
    Open strPath For Output As intFile
      Print #intFile, strText
    Close
    
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Editing a text file from VBA

    Maybe this?

    Sub EF972540_ClearTextFile()
        Dim fileName, toWrite As Variant
        Dim fd As Office.FileDialog
        Dim FS As FileSystemObject, a As TextStream
        
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .AllowMultiSelect = False
            .Title = "Select the text file"
            .Filters.Clear
            .Filters.Add "Text Files", "*.txt", 1
            If .Show = True Then
                fileName = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
        
        Set FS = New FileSystemObject
        Set a = FS.OpenTextFile(fileName, 1)
        toWrite = a.ReadLine
        a.Close
        
        Set a = FS.CreateTextFile(fileName, True)
        a.WriteLine toWrite
        a.Close
        
        Set a = Nothing
        Set FS = Nothing
    End Sub

  4. #4
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Editing a text file from VBA

    Hi guys, thanks for the responses!

    @Holger, I wasn't able to get your one to work for some reason, but @millz, yours worked perfectly.

    Could you possibly explain what the code is doing?

    Thanks again, both of you!

    Mike

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Editing a text file from VBA

    A quick summary:
    - Asks for text file
    - Open the selected text file
    - Read the first line of text, and store it in a variable
    - Create a new blank text file with the same directory\file name. Original file is overwritten
    - Writes that stored variable (first line of text) in the newly created text file

  6. #6
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Editing a text file from VBA

    @millz, great, thanks so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 09-06-2013, 05:31 AM
  2. Replies: 1
    Last Post: 10-21-2010, 06:26 AM
  3. Editing contents of a text file which is delimited.
    By FlyinJack in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-05-2009, 01:00 AM
  4. Editing text of an XML file
    By losbash@hotmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-05-2005, 11:05 PM
  5. [SOLVED] Trying to Open Excel File, But its says file is locked for editing
    By Smeeta Geary in forum Excel General
    Replies: 1
    Last Post: 09-20-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1