I want to create a folder and then save my automated text file into that folder in VBA. I wrote code that automatically creates a file with data in it and I want to save the file into a user-defined folder. Below is the code that I have tried but it does not work:
我想创建一个文件夹,然后将我的自动文本文件保存到VBA中的该文件夹中。我编写的代码自动创建一个包含数据的文件,我想将文件保存到用户定义的文件夹中。下面是我尝试过的代码,但它不起作用:
Sub test()
'Declaring variables
Dim equipID As String, destgroup As String, sourceparmname As String, descript As String
Dim lsb As Integer, msb As Integer, signed As String, sformat As String, units As String
Dim scalefact As Variant, numbits As Integer, decim As Integer
Dim ssystem As String
Dim vDB
Dim FName As String, stream As TextStream
Dim fso As Scripting.FileSystemObject, NewFolderPath As String
'Retrieve Target Folder Path From User
NewFolderPath = Application.GetSaveAsFilename("")
Set fso = New Scripting.FileSystemObject
If Not fso.FolderExists(NewFolderPath) Then
fso.CreateFolder NewFolderPath
End If
'Create txt file
Set stream = fso.CreateTextFile("NewFolderPath\test.txt")
..........
I would appreciate any inputs/suggestions :)
我很感激任何输入/建议:)
Thank you in advance!
先谢谢你!
1 个解决方案
#1
0
Your statement
你的陈述
Set stream = fso.CreateTextFile("NewFolderPath\test.txt")
is trying to create a file called "test.txt" within the folder called "NewFolderPath" within the current directory.
正试图在当前目录中名为“NewFolderPath”的文件夹中创建一个名为“test.txt”的文件。
You want to use
你想用
Set stream = fso.CreateTextFile(NewFolderPath & "\test.txt")
#1
0
Your statement
你的陈述
Set stream = fso.CreateTextFile("NewFolderPath\test.txt")
is trying to create a file called "test.txt" within the folder called "NewFolderPath" within the current directory.
正试图在当前目录中名为“NewFolderPath”的文件夹中创建一个名为“test.txt”的文件。
You want to use
你想用
Set stream = fso.CreateTextFile(NewFolderPath & "\test.txt")