vb 用commondialog1保存文件时,怎么能提示文件已打开
我的意思是如果我想覆盖文件,但是没注意此时文件已经是打开状态,这样的话会报错提示没有权限,请问判断文件是否已打开的语句是什么?以下是我的代码CommonDialog1.F...
我的意思是如果我想覆盖文件,但是没注意此时文件已经是打开状态,这样的话会报错提示没有权限,请问判断文件是否已打开的语句是什么?以下是我的代码
CommonDialog1.Filter = "文本文件(*.txt,*.dat)" '保存结果
CommonDialog1.ShowSave
If Len(CommonDialog1.FileName) = 0 Then Exit Sub
If Dir(CommonDialog1.FileName) <> "" Then a = MsgBox("文件已存在,是否覆盖?", vbOK, "提示")
If a = vbOK Then
Open CommonDialog1.FileName For Output As #1 '如果想覆盖一打开的文件,此处报错
ReDim cg(zds - 1)
For i = 0 To zds - 1
cg(i) = zdm(i) & "," & lc2(i) & "," & pj2(i) & "," & pj3(i) & "," & zdx(i) & "," & zdy(i)
Print #1, cg(i)
Next i
Close #1
Else
Exit Sub
End If
End Sub 展开
CommonDialog1.Filter = "文本文件(*.txt,*.dat)" '保存结果
CommonDialog1.ShowSave
If Len(CommonDialog1.FileName) = 0 Then Exit Sub
If Dir(CommonDialog1.FileName) <> "" Then a = MsgBox("文件已存在,是否覆盖?", vbOK, "提示")
If a = vbOK Then
Open CommonDialog1.FileName For Output As #1 '如果想覆盖一打开的文件,此处报错
ReDim cg(zds - 1)
For i = 0 To zds - 1
cg(i) = zdm(i) & "," & lc2(i) & "," & pj2(i) & "," & pj3(i) & "," & zdx(i) & "," & zdy(i)
Print #1, cg(i)
Next i
Close #1
Else
Exit Sub
End If
End Sub 展开
2个回答
展开全部
CommonDialog1.Filter = "文本文件(*.txt,*.dat)" '保存结果
CommonDialog1.ShowSave
If Len(CommonDialog1.FileName) = 0 Then Exit Sub
If Dir(CommonDialog1.FileName) <> "" Then a = MsgBox("文件已存在,是否覆盖?", vbOK, "提示")
If a = vbOK Then
on error goto err
Open CommonDialog1.FileName For Output As #1 '如果想覆盖一打开的文件,此处报错
ReDim cg(zds - 1)
For i = 0 To zds - 1
cg(i) = zdm(i) & "," & lc2(i) & "," & pj2(i) & "," & pj3(i) & "," & zdx(i) & "," & zdy(i)
Print #1, cg(i)
Next i
Close #1
Else
Exit Sub
err:
'
End If
这样就可以了
展开全部
可以用错误捕抓的方式
好吧!
on error goto ErrManage
CommonDialog1.Filter = "文本文件(*.txt,*.dat)" '保存结果
CommonDialog1.ShowSave
If Len(CommonDialog1.FileName) = 0 Then Exit Sub
If Dir(CommonDialog1.FileName) <> "" Then a = MsgBox("文件已存在,是否覆盖?", vbOK, "提示")
If a = vbOK Then
Open CommonDialog1.FileName For Output As #1 '如果想覆盖一打开的文件,此处报错
ReDim cg(zds - 1)
For i = 0 To zds - 1
cg(i) = zdm(i) & "," & lc2(i) & "," & pj2(i) & "," & pj3(i) & "," & zdx(i) & "," & zdy(i)
Print #1, cg(i)
Next i
Close #1
Else
Exit Sub
End If
ErrManage:
if err.number=0 then
rem donothing
elseif err.number=70 then
msgbox "错误代码:" & err.number & vbcrlf & "错误描述:" & err.description
exit sub
else
msgbox "错误代码:" & err.number & vbcrlf & "错误描述:" & err.description
exit sub
endif
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询