Export a textbox value as a text file

rdoulaghsingh

Board Regular
Joined
Feb 14, 2021
Messages
105
Office Version
  1. 365
Platform
  1. Windows
I am trying to export the notes from a single textbox on a worksheet called "CONTENTS" to a text file. The code works well, but if I click "cancel" on the saveasdialog box, then it creates a text file in the location with the name "False". I'm assuming I need a statement to handle if cancel is clicked, but nothing I've done is working so far. Any help would be appreciated. Thanks!

VBA Code:
Sub ExportMyNotes()
Dim TNotes As String
Dim WBname As String

On Error Resume Next

Application.ScreenUpdating = False

Dim FN As String
Dim FF As Integer

WBname = Replace(ActiveWorkbook.Name, ".xlsm", "")

TNotes = Worksheets("CONTENTS").TextBoxes("Notes").Text

If Worksheets("CONTENTS").TextBoxes("Notes").Text <> "" Then
    FN = Application.GetSaveAsFilename(WBname & " - Project Notes.txt", _
        "Text Files (*.txt), *.txt", , "Export File")
   
    FF = FreeFile
   
    Open FN For Output As #FF
        Print #FF, TNotes
    Close #FF
'End If

Else

Exit Sub

End If

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Untested, try:
VBA Code:
Sub EPN()

    Dim s As Variant: ReDim s(2)
    
    With ActiveWorkbook
        s(0) = Replace(.Name, ".xlsm", "") & " - Project Notes.txt"
        s(1) = .Sheets("CONTENTS").TextBoxes("Notex").Text
    End With
    
    If Len(s(1)) > 0 Then
        s(2) = Application.GetSaveAsFilename(s(0), "Text Files (*.txt), *.txt", , "Export File")
        If Len(s(2)) > 0 And LCase(s(2)) <> "false" Then Save_Notes s(2), s(1)
    End If
    
    Erase s
    
End Sub

Private Sub Save_Notes(s As String, c As String)
    
    Dim x As Integer
    
    x = FreeFile
    Open s For Output As #x
        Print #x, c
    Close #x
    
End Sub
 
Upvote 0
Please change this line
VBA Code:
If Len(s(2)) > 0 And LCase(s(2)) <> "false" Then Save_Notes s(2), s(1)
to
VBA Code:
If Len(s(2)) > 0 And LCase(s(2)) <> "false" Then Save_Notes CStr(s(2)), CStr(s(1))
 
Upvote 0
Please change this line
VBA Code:
If Len(s(2)) > 0 And LCase(s(2)) <> "false" Then Save_Notes s(2), s(1)
to
VBA Code:
If Len(s(2)) > 0 And LCase(s(2)) <> "false" Then Save_Notes CStr(s(2)), CStr(s(1))
This worked like a charm! Thanks @JackDanIce! I just had to change the name of the textbox control and it fired right up. One more question...Is it possible to target a default location for the saveasdialog box? Like would it be possible to launch by default in the Documents folder?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top