Excel 2016: Excel VBA, Copy Userform Textbox.value to a new text file

ajay_gajree

Well-known Member
Joined
Jul 16, 2011
Messages
518
Hi All

Can someone tell me how to copy a Userform Textbox.value to a new text file using VBA?

I would like to save the new text file to the current directory

Any pointers welcome!
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,005
Try...

Code:
    Dim saveToDirectory As String
    Dim saveAsFilename As String
    Dim fileNumber As Integer
    
    saveToDirectory = CurDir & "\"
    saveAsFilename = "sample.txt" 'change the file name accordingly
    
    fileNumber = FreeFile()
    Open saveToDirectory & saveAsFilename For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL] 
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL] , Me.TextBox1.Value
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL]
Note that if a file with the same name already exists, it will be overwritten.

Hope this helps!
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,655
Office Version
365
Platform
Windows
Another way (which also overwrites existing file)
Before running the code Go to VBA \ Tools \ References \ check box for "Microsoft Scripting Runtime"
I have assumed current directory is the folder of the active workbook (amend if that is not what you want)

In Userform code
Code:
Option Explicit

Private Sub CommandButton1_Click()
    WriteToTextFile (TextBox1.Value)
    Unload Me
End Sub

Private Sub WriteToTextFile(TextBoxText As String)
    Dim filePath As String, fileStream As TextStream, fso As FileSystemObject
    filePath = [COLOR=#000080]ActiveWorkbook.Path[/COLOR] & "\" & "[COLOR=#000080]MyTestFile.txt[/COLOR]"
    Set fso = New FileSystemObject
    Set fileStream = fso.CreateTextFile(filePath)
'write to file
    fileStream.WriteLine TextBoxText
    fileStream.Close
    If fso.FileExists(filePath) Then MsgBox filePath & " was created"
'tidy up
    Set fileStream = Nothing
    Set fso = Nothing
End Sub
refer this webpage
 
Last edited:

Forum statistics

Threads
1,081,899
Messages
5,361,928
Members
400,666
Latest member
UDLearning

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top