VB保存文件问题?谢谢了

来源:百度知道 编辑:UC知道 时间:2024/05/12 04:05:31
帮我看看,为什么不能点取消,一点取消就说路径/文件访问错误,请问要加点什么东西,谢谢了
CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #1
Print #1, Text10.Text
Close 1

VB中的对话框控件不论是,打开还是保存都需要一个完整的操作过程,按了保存而没有完成了保存的操作,就会出现错误,有两种方法可以不出错(我只知道两种)
CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1.ShowSave
if commondialog1.filename="" then exit sub
Open CommonDialog1.FileName For Output As #1
Print #1, Text10.Text
Close 1
end if
也可以在程序的前面加上错误处理语句:
on error goto connerror
CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #1
Print #1, Text10.Text
Close 1
connerror:
就这样,可以了。

检查一下CommonDialog1.CancelError就知道是否点了取消,点取消后CommonDialog1.FileName的值为vbNullString,所以会发生“路径/文件访问错误”
改成这样:
If Not CommonDialog1.CancelError Then
Open CommonDialog1.FileName For Output As #1
Print #1, Text10.Text
Close 1
End If

CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1