【VBA】Create text file

You can create text file!

PR

VBA Code

As an example, Create text file “hogehoge.txt”.

Option Explicit

Sub sampleProc()
    
    Dim fileFullPath As String
    Dim fileNo As Integer
    
    'Set "File path you want to create"
    fileFullPath = "C:\Users\user\Desktop\hogehoge.txt"

    'Get "Available file numbers"
    fileNo = FreeFile
    
    'Open File ※If file is not exist, Create a new file.
    Open fileFullPath For Output As #fileNo
    
    'Write File
    Print #fileNo, "hogehoge"

    'Close File
    Close #fileNo
    
End Sub

Set “File path you want to create”(Line 9).

Get “Available file numbers”(Line 12).

Open File (Line 15).
Write File(Line 18).
Close File(Line 21).

PR

Result

You created text file!

Result
Result
PR

FYI

For more information about following, see the following official online manual:

●Open statement


●Print statement


●Close statement


●FreeFile function

タイトルとURLをコピーしました