CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2001
    Posts
    3

    How do i create a text file using vb code?

    I have a program that determins whether a text file exists or not and works fine if it does exist.

    If the file does not exist, how do I write the vb code to create it?

    Thanks in advance...



    If Dir("C:\abc.txt") = "" then
    MsgBox "File with settings does not exist"
    else
    FileNum = FreeFile
    Open "c:\abc.txt" for input as #FileNum
    for i = 0 to 23
    input #FileNum, Reading
    Text1(i) = Reading
    next

    Close #FileNum
    End If





  2. #2
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: How do i create a text file using vb code?

    I prefer user FSO(File System Object):

    Sub OpenTextFileTest()
    Const ForAppending = 8
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    ' Note: Set the last parameter to True, it will automatically create a new text file if it doesn't exist
    Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, True)
    f.Write "Hello world!"
    f.Close
    End Sub

    Regards,

    Michi

  3. #3
    Join Date
    Feb 2001
    Posts
    54

    Re: How do i create a text file using vb code?

    Hello:

    You can use Commondialog to do it:

    private Sub Command1_Click()
    CommonDialog1.ShowSave
    MsgBox "My file to be saved:" & CommonDialog1.FileName
    End Sub





    private Sub Command2_Click()
    Open "c:\abc.txt" for output as #FileNum
    for i = 0 to 23
    input #FileNum,Reading
    Text1(i) = Reading
    next
    Close #FileNum




    Good Luck


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured