0

Hi All is there someone can help be on how to create a text files using multiline values of a textbox as filenames?

I have a multiline textbox in a userform in which I input multiple values. My code can create text file if I just input one value but if I input 2 or more nothing created. Can you help me identify which code is missing in my code just to create text files and the name of the files should be each values in the multiline textbox? I already tried searching but it looks like no topic as same as my concern. Thank you in advance for any help.

Below is my code.

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

sayjon
  • 71
  • 9
  • Use Split() to create an array from the text box content, then loop over that array and create your files. – Tim Williams Aug 28 '17 at 04:23
  • @TimWilliams: `Split()`on what;) @sayjon: copy the multiline text in a text editor (e.g.. Notepad++) and make non printable characters visible, then you see the problem. Remove the chars with `Replace()` or as Tim suggested `Split()`but then `Join()`. – BitAccesser Aug 28 '17 at 04:27
  • Probably `vbLf` would be what you need – Tim Williams Aug 28 '17 at 04:28
  • @TimWilliams: sorry, hit return accidently;), but it should be `vbCrLf`? To be safe I would first `Replace(myString,Chr(10),vbNullString)` then `Replace(myString,Chr(13),vbNullString)`. – BitAccesser Aug 28 '17 at 04:37
  • hi @TimWilliams and bitAccesser . I am just new in Vba is it posible to show me how to do that or just give me some example .. its complicated for me. Thank you ... – sayjon Aug 28 '17 at 04:52
  • @BitAccesser is it possible to give me some example to do that so that i can easily identiy how i will insert in my code. Thank you. – sayjon Aug 28 '17 at 05:06
  • Then start learning:) I have to improve myself too. First I just posted half comment and now I see that I misunderstood your needs. With 3 lines you want three files, not one as I thought. Use the [Split function](https://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx) as we suggested to split the lines to an array (before the `For Each`loop) then use the array instead of `TextLogistic`. – BitAccesser Aug 28 '17 at 05:06
  • @BitAccesser yes I need 3 files for each values and depends on how many I input . – sayjon Aug 28 '17 at 05:13
  • Hi @BitAccesser thanks for the link its now solved .. thank you so much.. – sayjon Aug 28 '17 at 05:31
  • Glad I could help. For sure, you will post your solution as answer;) – BitAccesser Aug 28 '17 at 06:18
  • Hi @BitAccesser below is the solution. Kindly check . Thank you. – sayjon Aug 28 '17 at 07:04

1 Answers1

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
sayjon
  • 71
  • 9
  • Nice! Some hints for bounty;). Define all vartypes explict,`Dim sExportFolder, sFN`get implict variant, but should be strings (as`key`and`sFN`). Use`Option Explicit`(all vars must be declared) on top of every module to prevent typos. No need to`Replace(TextLogistic, Chr(13), "")`, just `Split(TextLogistic,Chr(13) & Chr(10), "")`. Last one: check`sExportFolder & "\" & ListBox1 & sFN`for [illegal chars](https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names).. – BitAccesser Aug 28 '17 at 07:56
  • Hi @BitAccesser is it possible for you to rewrite my answer so that its more clear to me ? I am just really new in Vba that's why if it's work I am not aware to check if the syntax is wrong or correct . it will enlighten me more if I see the right syntax. thank you. – sayjon Aug 28 '17 at 08:16