请问如何用VB新建一个文本文件?

2024-11-21 21:28:13
推荐回答(5个)
回答1:

1、vb6新建文本文件有很多种办法。

2、以下示例使用open语句的output方式创建d:盘根目录下的TESTFILE.TXT文本文件。

Private Sub Command1_Click()
Open "d:\TESTFILE.TXT" For Output As #1
Print #1, "Hello world."
Close #1

End Sub

回答2:

sub command1_click()
filename$="c:\" & format(now, "yyyymm") & ".txt"
if len(dir(filename)) then msgbox "文件存在":exit sub
open filename for binary as #1
close #1
end sub

回答3:

Private Sub Command1_Click()
Dim nyr As String
On Error GoTo userERR
nyr = Mid(Date$, 1, 4) & Mid(Date$, 6, 2) & Mid(Date$, 9, 2)
Open "c:\" & nyr & ".txt" For Input As #1

Close
MsgBox ("文件存在")
userERR:
Open "c:\" & nyr & ".txt" For Output As #1

Close
End Sub

回答4:

Dim FileNum As Integer
Dim FileName As String
FileNum = FreeFile()
FileName = "c:\" & Format(Now, "YYYYMMDD") & ".txt"
If Dir(FileName) <> "" Then
MsgBox "文件存在"
Else
Open FileName For Output As FileNum
Close FileNum
End If

回答5:

Dim FileNum As Integer
Dim FileName As String
FileNum = FreeFile()
FileName = "c:\" & datetime.now.todate & ".txt"
If Dir(FileName) <> "" Then
MsgBox "文件存在"
Else
Openfile( filenum,openmode.open)
write(file,"filename.txt","")
Closefile(FileNum)
End If