입력에 따라 수동으로 추가하거나 수정 한 파일이 있습니다. 해당 파일에서 대부분의 내용이 반복적이므로 16 진수 값만 변경되므로 도구 생성 파일로 만들고 싶습니다.
txt 파일에 인쇄 될 c 코드를 작성하고 싶습니다 .
VBA 를 사용하여 .txt 파일 을 만드는 명령은 무엇 이며 어떻게 작성합니까?
답변
Ben의 대답 에 대해 자세히 설명하려면 :
당신에 대한 참조를 추가하는 경우 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
답변
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
여기에서 설명서를 참조하십시오.
답변
Open ThisWorkbook.Path & "\template.txt" For Output As #1
Print #1, strContent
Close #1
추가 정보:
- Microsoft Docs :
Open
성명 - Microsoft Docs :
Print #
성명 - Microsoft Docs :
Close
성명 - wellsr.com : VBA는
Print
Statement 로 텍스트 파일에 쓰기 - 사무실 지원 :
Workbook.Path
재산
답변
중복성이없는 쉬운 방법입니다.
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
답변
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()