VBA to add .txt file to folder giving :Invalid Qualifier error

Zerb

New Member
Joined
Dec 29, 2021
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Below is my code that is giving me an :Invalid Qualifier error. I can create a folder on the desktop just fine, then I want to be able to add .txt files to it, so I assumed using the variable for FolderPath would be the way to go but that does not seem to be the case. If I do a Debug.Print FolderPath I can see the exact path that I want, however if I use that FolderPath variable down on the line with Set Text File it does not work. What is the best way to accomplish this?

VBA Code:
Dim FolderName As String
Dim DesktopPath As String
Dim FolderPath As String
Dim FSOCreateFolder As Object
Dim DisplayFolderCreated As Boolean
        
    
'Find Desktop path location
DesktopPath = Environ("USERPROFILE") & "\Desktop\"
    
'Define folder name to create on the desktop
FolderName = "Test Folder"
    
'Folder Path
FolderPath = DesktopPath & FolderName
        
'Create FSO Object
Set FSOCreateFolder = CreateObject("Scripting.FileSystemObject")
    
'Check Specified Folder exists or not
If Not FSOCreateFolder.FolderExists(FolderPath) Then
    'Create Folder
    MkDir FolderPath
    DisplayFolderCreated = True
Else
    DisplayFolderCreated = False
End If
     
Debug.Print FolderPath
     
'----------//////////----------//////////----------//////////----------//////////

Dim FSOTxt As Object
Dim TextFile As Object
    
Set FSOTxt = CreateObject("Scripting.FileSystemObject")
 
Set TextFile = FSOTxt.CreateTextFile(FolderPath.txt, True, True)
 
TextFile.Write "Testing123"
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
CreateTextFile method needs a filename in that position, not only the path LINK so you will need to write something like this:
VBA Code:
Set TextFile = FSOTxt.CreateTextFile(FolderPath & "\" & "filename.txt", True, True)
 
Upvote 0
CreateTextFile method needs a filename in that position, not only the path LINK so you will need to write something like this:
VBA Code:
Set TextFile = FSOTxt.CreateTextFile(FolderPath & "\" & "filename.txt", True, True)
That did it. Thanks!
 
Upvote 0
Thanks for the positive feedback, glad I was able to help(y).
 
Upvote 0

Forum statistics

Threads
1,214,393
Messages
6,119,261
Members
448,880
Latest member
aveternik

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