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