Opening files and editing them

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hey guys,

I ran into a small problem that I can't figure out. The code lets a user choose a location, once chosen it goes through all the files in the folder and makes a txt named the same as the file.

Code:
Sub GetFileNames()
    Dim sFolder As String
    Dim MyFSO As FileSystemObject
    Dim MyFile As File
    Dim MyFolder As Folder
    Dim myFileName As Variant
    Dim a As Variant
    Dim f As Variant
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "Libraries\Documents"
        If .Show = -1 Then
            sFolder = .SelectedItems(1)
        End If
    End With
    Set MyFSO = New Scripting.FileSystemObject
    Set MyFolder = MyFSO.GetFolder(sFolder)
    For Each MyFile In MyFolder.Files
        Debug.Print MyFile.Name
        myFileName = Split(MyFile.Name, ".")
        Set a = MyFolder.CreateTextFile(myFileName(0) + ".txt", True)
        Set f = MyFSO.OpenTextFile(sFolder + "\" + MyFile.Name, 8, TristateFalse)
        f.WriteLine ("This is a test.")
        f.Close
    Next MyFile
End Sub

So once it runs, i expect the file to be created, opened, appended to and then closed.
If i run this code without creating a new sheet (commenting set a out) it runs with no problem and appends this is a test to each txt file.
But whenever i try to create the file and then open it, the text is never appended. Why is it so?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,215,214
Messages
6,123,666
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top