Example - Print # Statement
This first example loop to create three different files TEST1, TEST2, and TEST3 on your D drive. In each file, it writes two lines of text:
Sub Main() Dim I, FNum, FName ' Declare variables. For I = 1 To 3 FNum = FreeFile ' Determine next file number. FName = "D:\TEST" & FNum Open FName For Output As FNum ' Open file. Print #I, "This is test #" & I ' Write string to file. Print #I, "Here is another ";"line";I Next I Close ' Close all files. End Sub
This next example creates TESTFILE.txt on your D drive. It writes four lines of text to the file. Later, it then loops through all the lines of text building up a Msg variable with each subsequent line:
Sub Main() Dim FileData, Msg, NL ' Declare variables. NL = Chr(10) ' Define newline. Dim TESTFILE as String TESTFILE = "D:\TESTFILE.TXT" Open TESTFILE For Output As #1 ' Open to write file. Print #1, "This is a test of the Print # statement." Print #1,NL ' Print blank line to file. Print #1, "Zone 1", "Zone 2" ' Print in two print zones. Print #1, "With no space between" ;"." ' Print two strings together. Close MsgBox "The file " & TESTFILE & "is now created." Open TESTFILE for Input As #2 ' Open to read file. Do While Not EOF(2) Line Input #2, FileData ' Read a line of data. Msg = Msg & FileData & NL ' Construct message. MsgBox Msg Loop Close ' Close all open files. Kill TESTFILE ' Remove file from disk. MsgBox "The file " & TESTFILE & "is now deleted." End Sub