如何使用VBA创建和写入txt文件

dan*_*nny 110 vba text-files

我有一个文件,根据输入手动添加或修改.由于大多数内容在该文件中都是重复的,因此只有十六进制值发生变化,我想将其作为工具生成的文件.

我想编写将在.txt文件中打印的c代码.

使用VBA 创建.txt文件的命令是什么,以及如何写入它

Ben*_*Ben 157

使用FSO创建文件并写入文件.

Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile as Object
Set oFile = FSO.CreateTextFile(strPath)
oFile.WriteLine "test" 
oFile.Close
Set fso = Nothing
Set oFile = Nothing    
Run Code Online (Sandbox Code Playgroud)

请参阅此处的文档:

  • 当您直接引用Scripting Runtime时,您可以使用正确的类型:`Dim oFs As New FileSystemObject Dim oFile As TextStream (23认同)
  • 我不同意这个答案正在提倡不良的编码实践。使用像这样的后期绑定是完全可以接受的,并且在您不知道用户计算机上拥有哪个版本的 Microsoft 脚本运行时时非常有用。 (7认同)
  • @MarcusMangelsdorf 我听说过你,但我不想辩论。 (3认同)
  • 请注意,这个答案**促进了错误的编码实践**:问题是_不明确定义正确的变量types_以及_通过对其name_的字符串引用创建对象可能会导致您将来很难调试问题(例如,如果你拼错了名字的一部分).此外,通过不键入变量,您无法了解`FileSystemObject`必须提供的其他惊人方法.@Ben:请**考虑更新你的答案**以引导初学者[更好的方向](/sf/answers/3477222381/). (2认同)

小智 35

Open ThisWorkbook.Path & "\template.txt" For Output As #1
Print #1, strContent
Close #1
Run Code Online (Sandbox Code Playgroud)

更多信息:

  • 我更喜欢这种方法用于FSO方法,因为它不需要外部引用,而且非常短.虽然我建议使用FreeFile来获取文件编号,而不是将其硬编码为#1. (3认同)
  • 我更喜欢这种方法,也适用于平凡的文本文件编写.至少自1981年以来,这些陈述一直是BASIC语言的一部分. (3认同)
  • 请写下答案并附上一些解释和细节。 (2认同)
  • 这很好。之前我从未见过“ Open somePath For Output As#1”语法,该文档对此进行了记录:https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/open-statement (2认同)
  • 关于@phrebh关于使用FreeFile而不是硬编码#1的评论,请参阅https://wellsr.com/vba/2016/excel/vba-freefile-for-foolproof-file-IO/ (2认同)

pel*_*los 31

一种简单的方法,有很多冗余.

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim Fileout As Object
    Set Fileout = fso.CreateTextFile("C:\your_path\vba.txt", True, True)
    Fileout.Write "your string goes here"
    Fileout.Close
Run Code Online (Sandbox Code Playgroud)

  • 是否可以使用文件选择器来设置路径? (2认同)

Mar*_*orf 25

详细阐述Ben的答案(因为似乎不允许改进它):

如果您添加引用Microsoft Scripting Runtime并正确键入变量fso,您可以利用自动完成(Intellisense)并发现其他强大的功能FileSystemObject.

这是一个完整的示例模块:

Option Explicit

' Go to Tools -> References... and check "Microsoft Scripting Runtime" to be able to use
' the FileSystemObject which has many useful features for handling files and folders
Public Sub SaveTextToFile()

    Dim filePath As String
    filePath = "C:\temp\MyTestFile.txt"

    ' The advantage of correctly typing fso as FileSystemObject is to make autocompletion
    ' (Intellisense) work, which helps you avoid typos and lets you discover other useful
    ' methods of the FileSystemObject
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim fileStream As TextStream

    ' Here the actual file is created and opened for write access
    Set fileStream = fso.CreateTextFile(filePath)

    ' Write something to the file
    fileStream.WriteLine "something"

    ' Close it, so it is not locked anymore
    fileStream.Close

    ' Here is another great method of the FileSystemObject that checks if a file exists
    If fso.FileExists(filePath) Then
        MsgBox "Yay! The file was created! :D"
    End If

    ' Explicitly setting objects to Nothing should not be necessary in most cases, but if
    ' you're writing macros for Microsoft Access, you may want to uncomment the following
    ' two lines (see https://stackoverflow.com/a/517202/2822719 for details):
    'Set fileStream = Nothing
    'Set fso = Nothing

End Sub
Run Code Online (Sandbox Code Playgroud)

  • 早期绑定有很多优点(它也更快)但是,我觉得您没有强调**后期绑定与版本无关**并且不需要创建引用这一事实。这对于重新分发的任何代码都至关重要 (4认同)