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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11503174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:33:05  来源:igfitidea点击:

How to create and write to a txt file using VBA

vbatext-files

提问by danny

I have a file which is manually added or modified based on the inputs. Since most of the contents are repetitive in that file, only the hex values are changing, I want to make it a tool generated file.

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

I want to write the c codes which are going to be printed in that .txtfile.

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

What is the command to create a .txtfile using VBA, and how do I write to it

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

回答by Ben

Use FSO to create the file and write to it.

使用 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    

See the documentation here:

请参阅此处的文档:

回答by Bhanu Sinha

Open ThisWorkbook.Path & "\template.txt" For Output As #1
Print #1, strContent
Close #1


More Information:

更多信息:

回答by pelos

an easy way with out much redundancy.

一种没有太多冗余的简单方法。

    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

回答by Marcus Mangelsdorf

To elaborate on Ben's answer:

详细说明本的回答

If you add a reference to Microsoft Scripting Runtimeand correctly type the variable fsoyou can take advantage of autocompletion(Intellisense) and discover the other great features of FileSystemObject.

如果添加Microsoft Scripting Runtime对变量fso的引用并正确键入该变量,则可以利用自动完成功能(智能感知)并发现FileSystemObject.

Here is a complete example module:

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

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

回答by Zack Brightman

Dim SaveVar As Object

Sub Main()

    Console.WriteLine("Enter Text")

    Console.WriteLine("")

    SaveVar = Console.ReadLine

    My.Computer.FileSystem.WriteAllText("N:\A-Level Computing17!\PPE\SaveFile\SaveData.txt", "Text: " & SaveVar & ", ", True)

    Console.WriteLine("")

    Console.WriteLine("File Saved")

    Console.WriteLine("")

    Console.WriteLine(My.Computer.FileSystem.ReadAllText("N:\A-Level Computing17!\PPE\SaveFile\SaveData.txt"))
    Console.ReadLine()

End Sub()