Results 1 to 5 of 5

Thread: [RESOLVED] Help creating Text Files using VBA Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Resolved [RESOLVED] Help creating Text Files using VBA Excel

    I am trying to figure out a way to create a simple text file from excel (using VBA). The code below comes close but with a hitch. The stext string seems to need the quotations which presents a problem.

    I would like an example of VBA code that can create a text file and pass a string without quotations.

    Dim sFile As String
    Dim sText As String
    Dim iFileNum As Integer

    sFile = "C:\(path)\Textfile.txt"

    sText = "myString"

    iFileNum = FreeFile
    Open sFile For Output As iFileNum
    Write #iFileNum, sText
    Close #iFileNum

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Help creating Text Files using VBA Excel

    Hmmm, I think you need to say more. Welcome to the forums, by the way. If the value of a text variable is assigned within VBA, it has to be surrounded by quotes. Otherwise it will be interpreted as the name of a variable or cause an error. Perhaps you can explain why it poses a problem and explain a little more what you are trying to do?

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Re: Help creating Text Files using VBA Excel

    Hey, Thanks for the explaination. The reason the quotations pose a problem is because the quotations themselves get passed to the text file along with the string.
    What I am trying to do is create a text file with specified text (syntax for a console application) from Excel.

    So if anyone could give me an example of creating a text file (Hello World maybe?)using VBA from Excel, I would appreciate it greatly. i dont really care about the code i provided (whether it gets used or not), I just need a method to this end. Thanks

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Help creating Text Files using VBA Excel

    Just change Write to Print, and it should write to the file without quotes.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Resolved Re: Help creating Text Files using VBA Excel

    Problem Solved! Thank You!

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