如何创建并写入使用VBA一个txt文件(How to create and write to a t

2019-06-17 11:39发布

我有基于输入手动添加或修改的文件。 由于大部分的内容是在该文件中重复的,只有十六进制值都在变化,我希望把它的工具生成的文件。

我想写这会在.txt文件要打印的C代码。

什么是创建使用VBA .txt文件的命令,我怎么写吧

Answer 1:

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

看到这里的文档:

  • http://technet.microsoft.com/en-us/library/ee198742.aspx
  • http://technet.microsoft.com/en-us/library/ee198716.aspx


Answer 2:

一个简单的方法有太大的冗余。

    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


Answer 3:

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

更多信息:

  • 微软文档: Open声明
  • 微软文档: Print #声明
  • 微软文档: Close声明
  • wellsr.com: VBA写入文本文件与Print声明
  • 办公支持: Workbook.Path财产


Answer 4:

为了阐述本的答案 :

如果您添加到参考Microsoft Scripting Runtime ,并输入正确的变量FSO可以利用自动完成 (智能感知) 发现的其他强大功能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


Answer 5:

Dim SaveVar As Object

Sub Main()

    Console.WriteLine("Enter Text")

    Console.WriteLine("")

    SaveVar = Console.ReadLine

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

    Console.WriteLine("")

    Console.WriteLine("File Saved")

    Console.WriteLine("")

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

End Sub()


文章来源: How to create and write to a txt file using VBA