以前自己写的一套VB编程基础系列教程,适合VB初学者。

从部件里添加一个CommonDialog控件(不会添加CommonDialog控件的点这里),写入以下代码即可!

CommonDialog1.Filter = "文本文档(*.txt)|*.txt|ASP文件(*.asp)|*.asp|VBS脚本(*.vbs)|*.vbs|所有文件(*.*)|*.*" '显示要保存的所有类型
CommonDialog1.filterindex = 1 '默认保存文件的类型
CommonDialog1.Action = 2 '1为打开,2为保存
Dim str As String
str = CommonDialog1.FileName 'str等于输入保存的文件名
If (Trim(str) <> "") Then
   Set fso = CreateObject("scripting.filesystemobject")
   Set txt = fso.createtextfile(str)
   txt.write (Text1.Text)
   MsgBox "文件保存成功!", vbOKOnly + vbInformation, "操作提示"
End If