+ Reply to Thread
Results 1 to 4 of 4

Help - Can Excel create a text file??

  1. #1
    Registered User
    Join Date
    12-18-2005
    Posts
    3

    Help - Can Excel create a text file??

    Hi, Can we programme excel to create a Text file in the folder using VBA? For example: I want to programme VBA to create a file call Mysave.txt inside one of the folder? Can that be done?? Pls help me... Thanks alot..

  2. #2
    Chip Pearson
    Guest

    Re: Help - Can Excel create a text file??

    Try something like the following:


    Dim FName As String
    Dim Ndx As Long
    FName = "H:\Test\test.txt" '<< CHANGE
    Open FName For Output As #1
    For Ndx = 1 To 10
    Print #1, Ndx
    Next Ndx
    Close #1


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com




    "Acube" <Acube.208xny_1134927002.6797@excelforum-nospam.com>
    wrote in message
    news:Acube.208xny_1134927002.6797@excelforum-nospam.com...
    >
    > Hi, Can we programme excel to create a Text file in the folder
    > using
    > VBA? For example: I want to programme VBA to create a file call
    > Mysave.txt inside one of the folder? Can that be done?? Pls
    > help me...
    > Thanks alot..
    >
    >
    > --
    > Acube
    > ------------------------------------------------------------------------
    > Acube's Profile:
    > http://www.excelforum.com/member.php...o&userid=29734
    > View this thread:
    > http://www.excelforum.com/showthread...hreadid=494451
    >




  3. #3
    Edward Ulle
    Guest

    Re: Help - Can Excel create a text file??

    If you want to be a little more elegant this will present the typical
    windows file dialog box. The user can input a new file name and then
    write to the text stream.

    Just presenting another option. Notice you have to refence the
    Microsoft Scripting Runtime and I typically close the text stream before
    exiting the module.

    Option Explicit

    ' Requires Reference to Microsoft Scripting Rntime

    Sub Test()

    Dim fsoFileSystemObject As FileSystemObject
    Dim strFileName As String
    Dim tsTextStream As TextStream

    Set fsoFileSystemObject = CreateObject("Scripting.FileSystemObject")
    strFileName = Application.GetSaveAsFilename()
    Set tsTextStream = fsoFileSystemObject.CreateTextFile(strFileName)

    tsTextStream.WriteLine "Line 1"
    tsTextStream.WriteLine "Line 2"
    tsTextStream.WriteLine "Line 3"

    tsTextStream.Close

    End Sub




    *** Sent via Developersdex http://www.developersdex.com ***

  4. #4
    donnie.sims@gmail.com
    Guest

    Re: Help - Can Excel create a text file??

    Try this
    ActiveCell.FormulaR1C1 = Range("A1")
    ActiveCell.FormulaR1C1 = Range("A2")

    Open "C\text.txt" For Append As #1
    Write #1, Range("A1"),1Range("A2")
    Close #1


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1