vb中创建excel,出现“类workbook的saveas方法无效”错误
用vb生成excel并保存,若已有该同名文件,则会弹出一个对话框问是否要覆盖,若选者“是”,若选择“否”,这会报上述错误。代码如下:dimxlbook,fnfn=App....
用vb生成excel并保存,若已有该同名文件,则会弹出一个对话框问是否要覆盖,若选者“是”,若选择“否”,这会报上述错误。代码如下:
dim xlbook, fn
fn = App.Path + "\" + Label3.Caption + "--" + Label10.Caption + ".xls"
Set xls = CreateObject("excel.Application")
Set xlbook = xls.Workbooks.Add
Set Sheet = xlbook.Sheets(1)
Sheet.Range("a1") = Label10.Caption
Sheet.Range("b1") = Label11.Caption
Sheet.Range("c1") = Label12.Caption
Sheet.Range("d1") = Label3.Caption
Sheet.Range("e1") = Label4.Caption
ActiveWorkbook.Close savechanges = False
xls.Workbooks(1).SaveAs fn
xls.Quit 展开
dim xlbook, fn
fn = App.Path + "\" + Label3.Caption + "--" + Label10.Caption + ".xls"
Set xls = CreateObject("excel.Application")
Set xlbook = xls.Workbooks.Add
Set Sheet = xlbook.Sheets(1)
Sheet.Range("a1") = Label10.Caption
Sheet.Range("b1") = Label11.Caption
Sheet.Range("c1") = Label12.Caption
Sheet.Range("d1") = Label3.Caption
Sheet.Range("e1") = Label4.Caption
ActiveWorkbook.Close savechanges = False
xls.Workbooks(1).SaveAs fn
xls.Quit 展开
1个回答
展开全部
你的代码逻辑还不完整,文件已经存在并且不覆盖时你准备怎么处理?
一种处理方式是忽视,即如果选择“否”,保存过程中断,具体代码:
记录弹出提示的错误号;增加错误处理,将这个错误屏蔽掉
dim xlbook, fn
on error goto errH
fn = App.Path + "\" + Label3.Caption + "--" + Label10.Caption + ".xls"
Set xls = CreateObject("excel.Application")
Set xlbook = xls.Workbooks.Add
Set Sheet = xlbook.Sheets(1)
Sheet.Range("a1") = Label10.Caption
Sheet.Range("b1") = Label11.Caption
Sheet.Range("c1") = Label12.Caption
Sheet.Range("d1") = Label3.Caption
Sheet.Range("e1") = Label4.Caption
ActiveWorkbook.Close savechanges = False
xls.Workbooks(1).SaveAs fn
xls.Quit
exit sub
errH:
if err.number=xxx then ' 这里你需要获取错误代码
'如果是忽视,这里不做处理
end if
一种处理方式是忽视,即如果选择“否”,保存过程中断,具体代码:
记录弹出提示的错误号;增加错误处理,将这个错误屏蔽掉
dim xlbook, fn
on error goto errH
fn = App.Path + "\" + Label3.Caption + "--" + Label10.Caption + ".xls"
Set xls = CreateObject("excel.Application")
Set xlbook = xls.Workbooks.Add
Set Sheet = xlbook.Sheets(1)
Sheet.Range("a1") = Label10.Caption
Sheet.Range("b1") = Label11.Caption
Sheet.Range("c1") = Label12.Caption
Sheet.Range("d1") = Label3.Caption
Sheet.Range("e1") = Label4.Caption
ActiveWorkbook.Close savechanges = False
xls.Workbooks(1).SaveAs fn
xls.Quit
exit sub
errH:
if err.number=xxx then ' 这里你需要获取错误代码
'如果是忽视,这里不做处理
end if
追问
当我用了上述代码后,点击运行后,每次先跳出一个对话框,让你另存为,点击否再自动生成一个文件
好了,我把on error 往下移就好了,谢谢了
追答
嗯,我只是提供了一种思路,代码并不完整。错误处理里面还需要进一步处理,另外也需要将已经生成的对象销毁
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询