Private Sub Command1_Click()
Dim filename As String
filename = App.Path & "\" & Text1 & ".txt" '创建的文件格式.
If Dir(filename) = "" Then '判断文件是否存在,不存在就创建,存在就不创建
Open filename For Append As #1
Close #1
MsgBox "创建成功。", vbInformation, "成功。"
Else
MsgBox "文件已存在.", vbInformation, "已存在在文件."
End If
End Sub
1、vb使用open语句来打开要操作文件,使用open语句的output模式创建文本文件。
2、示例代码:
Private Sub Command1_Click()
Open "d:\test.txt" For Output As #1
Print #1, "Hello world!"
Close #1
MsgBox "ok"
End Sub
在窗体上 添加一个 command1 和一个textbox1
Private Sub Command1_Click()
Dim filename, str As String
filename = Text1.Text
str = "123456"
Open App.Path & "\" & filename & ".txt" For Output As #1
Print #1, str
MsgBox "文件自动创建在VB默认的位置"
Close #1
End Sub
open "c:\" & text1.text & ".txt" for binary as #1
put #1,,"你创建了txt文本"
close#1
傻傻的疯猪
他的回答很好