因为打开方式写的是output,output总会把原来的同名文件删除,再新建一个。所以能看到原来a.txt的内容都被删除了。 另外使用文件编号,也让代码变得更加清晰可读。 2. 在同一行输入 Sub t2() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Print #1, "产品名称"; '←变化在...
Open "c:\示例文件.txt" For Input As #1 Open "c:\示例文件.txt" For Output As #1 Open "c:\示例文件.txt" For Append As #1 Open "c:\示例文件.txt" For Binary As #1 Open "c:\示例文件.txt" For Random As #1 注释:如果打开不止一个文件,把文件号1改成FreeFile,本篇文章都默认读取1个。
备注:如果你只明确了文件号码,VBA就会在打开的文件里写入一个空行。 下面我们通过一个实例来演示数据是如何写入文件的: 代码: Sub mynznewEntry()Dim newname As String Dim sex As String Dim newbirthdate As Date Dim news As Integer Open "E:\nz\work\文章\人员表单最新版.txt" For Output As #1...
Dim strFilePath As String '创建的文件的路径和名字 strFilePath="C:\MyFile.txt"'确定下一个可用的文件序号 iFileNumber=FreeFile '打开文本文件 Open strFilePath For Output As iFileNumber '写入文本 Print #iFileNumber,"你好!"Print #iFileNumber,"这是由VBA创建的文本文件."Print #iFileNumber,"完美Ex...
Open FileName For Output As #I ' 打开文件。 Print #I, "This is a test." ' 将字符串写入文件。 Next I Close ' 将三个已打开的文件全部关闭。 1. 2. 3. 4. 5. 6. 7. 3、Reset 语句 语法:Reset 功能:关闭所有用 Open 语句打开的磁盘文件。
Open FileName For Output As #I ' 打开文件。 Print #I, "This is a test." ' 将字符串写入文件。 Next I Close ' 将三个已打开的文件全部关闭。 3、Reset 语句 语法:Reset 功能:关闭所有用 Open 语句打开的磁盘文件。 说明:Reset 语句关闭 Open 语句打开的所有活动文件,并将文件缓冲区的所有内容写入...
Open"F:\test.txt"ForOutputAs#1'打开输出文件。Print#1,"huo","chang"Print#1,233234Print#1,"huo","chang-chang-chang-changchangchangchangchang",Print#1,"huo", Close #1End Sub Line input方法: Open"f:\test.txt"ForInputAs#1DoWhileNotEOF(1)'循环至文件尾。'MyChar = input(1, #1) ' 读入一...
Dim fileNum As Integer fileNum = FreeFile Open "C:\path\to\file.txt" For Output As fileNum Print #fileNum, "Hello, world!" Close fileNum ``` 在上面的示例代码中,首先定义一个整型变量`fileNum`表示文件编号,然后使用`Open`语句打开指定的文件,并指定打开模式为`Output`,表示以输出方式打开文件...
Open"TESTFILE"ForOutputAs#1 ' Open file for output. Write #1,"Hello World",234' Write comma-delimited data.Write#1, ' Write blank line. Dim MyBool, MyDate, MyNull, MyError ' Assign Boolean, Date, Null, and Error values. MyBool = False : MyDate = #February12,1969# : MyNull = ...
Open "F:/test.txt" For Output As #1 ' 打开输出文件。 Print #1, "This is a test" ' 将文本数据写入文件。 Print #1, ' 将空白行写入文件。 Print #1, "Zone 1"; Tab; "Zone 2" ' 数据写入两个区(print zones)。 Print #1, "Hello"; " "; "World" ' 以空格隔开两个字符串。