Print # 语句

将设置了显示格式的数据写入顺序文件。

语法

打印#filenumber, [ outputlist ]

Print # 语句的语法包含以下部分:

Part 说明
filenumber 必填。 任何有效的文件编号。
outputlist 可选。 要打印的表达式或表达式列表。

设置

outputlist参数设置包括:

[{ Spc (n) | Tab [ (n) ]}] [ expression ] [ charpos ]

Setting 说明
Spc (n) 用于在输出中插入空格,其中 n 为要插入的空格数。
Tab (n) 用于将插入点定位到绝对列号,其中 n 为列号。 使用不带参数的 Tab 可将插入点定位到下一个打印区域的起始位置。
expression 要打印的数字表达式或字符串表达式。
charpos 指定下一个字符的插入点。 使用分号可将插入点定位到上一个显示字符的正后方。 使用 Tab (n) 将插入点定位到绝对列号。 使用不带参数的 Tab 可将插入点定位到下一个打印区域的起始位置。 如果省略 charpos,将在下一行上打印下一个字符。

备注

使用 Print # 写入的数据通常从具有 Line Input # 或 Input # 的文件中读取。

如果省略 outputlist 并在 filenumber 后仅包括一个列表分隔符,则会在文件中打印一个空行。

可以使用空格或分号分隔多个表达式。 空格与分号具有相同的作用。

对于 Boolean 数据,将打印 True 或 False。 不管区域设置如何,都不翻译 True 和 False 关键字。

日期 数据通过使用系统识别的标准短日期格式写入文件。 当日期或时间组件缺失或为零时,仅提供的部分将被写入到文件中。

如果 outputlist 数据为 Empty,则不向文件中写入任何内容。 但是,如果 outputlist 数据为 Null,则将 Null 写入文件。

对于 “错误 数据”,输出显示为 Errorerrorcode。 不管区域设置如何,都不翻译 Error 关键字。

使用 Print # 写入文件的所有数据都是国际感知的;也就是说,使用适当的小数分隔符正确设置数据的格式。

由于 Print # 会将数据的图像写入文件,因此必须对数据进行分隔才能正确打印。 如果使用不带参数的 Tab 将打印位置移到下一个打印区域,Print # 还会将打印区域之间的空格写入文件。

注意

如果希望将来使用 Input # 语句从文件读取数据,请使用 Write # 语句而不是 Print # 语句将数据写入文件。 使用 Write # 通过正确分隔每个单独数据字段来确保其完整性,以便可以使用 Input #将其读回。 使用 写入 # 还可以确保可以在任何区域设置中正确读取它。

示例

此示例使用 Print # 语句将数据写入文件。

Open "TESTFILE" For Output As #1 ' Open file for output. 
Print #1, "This is a test" ' Print text to file. 
Print #1, ' Print blank line to file. 
Print #1, "Zone 1"; Tab ; "Zone 2" ' Print in two print zones. 
Print #1, "Hello" ; " " ; "World" ' Separate strings with space. 
Print #1, Spc(5) ; "5 leading spaces " ' Print five leading spaces. 
Print #1, Tab(10) ; "Hello" ' Print word at column 10. 
 
' Assign Boolean, Date, Null and Error values. 
Dim MyBool, MyDate, MyNull, MyError 
MyBool = False : MyDate = #February 12, 1969# : MyNull = Null 
MyError = CVErr(32767) 
' True, False, Null, and Error are translated using locale settings of 
' your system. Date literals are written using standard short date 
' format. 
Print #1, MyBool ; " is a Boolean value" 
Print #1, MyDate ; " is a date" 
Print #1, MyNull ; " is a null value" 
Print #1, MyError ; " is an error value" 
Close #1 ' Close file. 

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。