468,316 Members | 2,131 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

VBA code to save a text file


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.


Below is my code.

Dim dlgSaveAs As FileDialog

Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)

With dlgSaveAs
.InitialFileName = Format(Date, "yyyy-mm-dd") & "txtdelimited.txt"
End With

Jul 29 '08 #1
1 1792
112 Expert 100+
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 FileSystemObject 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.
Expand|Select|Wrap|Line Numbers
  1. Sub SaveFile(FileName As String)
  2.     Dim objFso As FileSystemObject
  3.     Set objFso = New FileSystemObject
  4.     objFso.CreateTextFile FileName
  5.     Set objFso = Nothing
  6. 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.
Jul 29 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Daveyk0 | last post: by
5 posts views Thread by Daniel Corbett | last post: by
11 posts views Thread by mistral | last post: by
2 posts views Thread by nuhura01 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.