PDA

View Full Version : Solved: Create User Defined Text File



MrRhodes2004
04-20-2007, 09:47 AM
Sub CreateAfile()
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile("c:\testfile.txt", True)
f.WriteLine ("This is a test.")
f.Close
End Sub

I can use VBA to Create a text file via the manner above. But the problem is that the programmer dictates the location and file name in the code. Is there a way to have the user define the folder and file name with a dialog box? If so, how?

Andy Pope
04-20-2007, 12:04 PM
Use the GetSaveAsFilename dialog.

Sub CreateAfile()
Dim vntSaveName as Variant

vntSaveName = application.GetSaveAsFilename
if vntSaveName = "False" then exit sub

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(vntSaveName, True)
f.WriteLine ("This is a test.")
f.Close
End Sub

MrRhodes2004
04-20-2007, 12:38 PM
That worked nicely.

Thanks!