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)
请参阅此处的文档:
小智 35
Open ThisWorkbook.Path & "\template.txt" For Output As #1
Print #1, strContent
Close #1
Run Code Online (Sandbox Code Playgroud)
Open声明 Print #声明Close声明PrintStatement 写入文本文件Workbook.Path财产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)
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)
| 归档时间: |
|
| 查看次数: |
447421 次 |
| 最近记录: |