Search code examples
excelvbatext-files

Use Path variable with an object created by CreateTextFile (VBA)


I'm trying to use the path variable with an object created via but I'm getting an error 438: Object Doesn’t Support this Property or Method:

Dim FSO As Object
Dim Text FileAs Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TextFile= FSO.CreateTextFile(ThisWorkbook.Path & "\" & FSO.GetBaseName(ThisWorkbook.FullName) & "_TextFile" & ".txt")
Debug.Print TextFile.Path 

What's the problem here? Is there any way to get the path from the created object directly? I'm trying to avoid creating an additional variable for the path.


Solution

  • The FSO.CreateTextFile "Creates a specified file name and returns a TextStream object that can be used to read from or write to the file." There isn't a Path property in TextStream so the Debug.Print TextFile.Path return error, after of course correcting the two mistakes I pointed out in the comment

    https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/createtextfile-method