使用多行文本框值创建文本文件作为文件名vba

时间:2017-08-28 03:33:25

标签: vba

大家好,是否有人可以帮助您如何使用文本框的多行值作为文件名创建文本文件?

我在userform中有一个多行文本框,我输入了多个值。如果我只输入一个值,但是如果输入2个或更多,则我的代码可以创建文本文件。你可以帮我识别我的代码中缺少哪些代码来创建文本文件,文件名应该是多行文本框中的每个值吗?我已经尝试过搜索,但看起来没有像我关心的话题一样。预先感谢您的任何帮助。

以下是我的代码。

Private Sub CREATE_REQ_Click()

    Dim sExportFolder, sFN
    Dim oFS As Object
    Dim oTxt As Object

    sExportFolder = TextBox1  ' "D:\TEST\REQ_FILES_CREATED_HERE"

    Set oFS = CreateObject("Scripting.Filesystemobject")
    For Each strLine In TextLogistic

    'Add .txt to the article name as a file name

    sFN = "-" & TextLogistic.Value & ".req"

 Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & ListBox1 & sFN, 2, True)
    oTxt.Write combo1.Value
    oTxt.Close
    Next

End Sub

enter image description here

1 个答案:

答案 0 :(得分:1)

Here's the solution for the above issue.

Private Sub CREATE_REQ_Click()

Dim sExportFolder, sFN
Dim oFS As Object
Dim oTxt As Object
Dim v As Variant
Dim vlist As Variant
Dim key As Variant


  Set oFS = CreateObject("Scripting.Filesystemobject")
  vlist = Split(Replace(TextLogistic, Chr(13), ""), Chr(10))

 For Each v In vlist
  key = Trim(v)

  sFN = "-" & key & ".req"
  Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & ListBox1 & sFN, 2, True)
  oTxt.Write combo1.Value
  oTxt.Close

Next

End Sub