Writing variable to a file using macro

时光怂恿深爱的人放手 提交于 2020-06-18 04:17:43

问题


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:


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


来源:https://stackoverflow.com/questions/38314900/writing-variable-to-a-file-using-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!