Writing variable to a file using macro

时间:2016-07-11 19:40:11

标签: vba excel-vba macros excel

I need to write data from a macro to a text file. I am using the below code:

VAR_ABC = "Deployment1.txt"
Dim FlName As String
filesize = FreeFile()

Open FlName For Output As #filesize
   Write #filesize, "Hello World!"
   Write #filesize, "" & VAR_ABC;
 Close #filesize

I have below questions:

  1. The output in my file contains double quotes i.e. "Hello World" "Deployment1.sh" How to get rid of these double quotes in my text file?
  2. Is there a way to write multiple lines in a better way(with new line character) than using Write again and again?

  3. If I am to use variables, is it mandatory to use them like: "" & VarName. Is it not possible to have only VarName in Write Command.

Thanks in Advance!

1 个答案:

答案 0 :(得分:0)

Replace Write with Print.

Sub test()
    VAR_ABC = "Deployment1.txt"
    Dim FlName As String
    filesize = FreeFile()

    Open "C:\Temp\Hello.txt" For Output As #filesize
       Print #filesize, "Hello World!"
       Print #filesize, VAR_ABC;
     Close #filesize
End Sub