VBA code to save a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AccessHunter
    New Member
    • Nov 2007
    • 77

    VBA code to save a text file

    Hi,

    I have written a code to display the Save As dialog box for my users, with a default file name. The code works and displays the dialog box with the default name. But when I click the Save button on the dialog box its not actually doing the saving. I am sure some of you might have faced the same issue, please help.

    Thanks

    Below is my code.

    Dim dlgSaveAs As FileDialog

    Set dlgSaveAs = Application.Fil eDialog(msoFile DialogSaveAs)

    With dlgSaveAs
    .InitialFileNam e = Format(Date, "yyyy-mm-dd") & "txtdelimited.t xt"
    End With

    dlgSaveAs.Show
  • janders468
    Recognized Expert New Member
    • Mar 2008
    • 112

    #2
    The Filedialog is typically a way to communicate with the users (i.e. let them name the file) but it's not an object for working with a file per se, it's just a convenient interface. The below code is a subroutine to save a file. There are many ways to do this, I prefer the FileSystemObjec t but to make the below code work you will need to make a reference to the Microsoft Scripting Runtime or some other library this object is contained in.
    Code:
    Sub SaveFile(FileName As String)
        Dim objFso As FileSystemObject
        Set objFso = New FileSystemObject
        objFso.CreateTextFile FileName
        Set objFso = Nothing
    End Sub
    If you want to combine this with the filedialog then you can just pass the name listed in the filedialog to this subroutine. Like I said there are many ways to do it and this doesn't include any error handling. But it may help.

    Comment

    Working...