By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,923 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

VBA code to save a text file

P: 77
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.FileDialog(msoFileDialogSaveAs)

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

dlgSaveAs.Show
Jul 29 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 112
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
  7.  
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.