如何创建文件夹,然后将生成的文本文件保存在VBA中的该文件夹中?
问题描述:
我想创建一个文件夹,然后将我的自动文本文件保存到VBA中的该文件夹中。我编写了自动创建一个包含数据的文件的代码,我想将该文件保存到用户定义的文件夹中。下面是我试过的代码,但它不工作:如何创建文件夹,然后将生成的文本文件保存在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")
..........
我将不胜感激任何输入/建议:)
预先感谢您!
答
你的陈述
Set stream = fso.CreateTextFile("NewFolderPath\test.txt")
正试图创建一个名为“test.txt的”在当前目录中称为“NewFolderPath”的文件夹中的文件。
你想用
Set stream = fso.CreateTextFile(NewFolderPath & "\test.txt")
真棒!我知道我错过了一些小事。非常感谢你 :) – Jesus