如何创建文件夹,然后将生成的文本文件保存在VBA中的该文件夹中?

时间:2017-10-16 02:41:08

标签: excel vba excel-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")

..........

我很感激任何意见/建议:)

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您的陈述

Set stream = fso.CreateTextFile("NewFolderPath\test.txt")

正在尝试创建名为" test.txt"的文件。在名为" NewFolderPath"的文件夹中在当前目录中。

您想要使用

Set stream = fso.CreateTextFile(NewFolderPath & "\test.txt")