vb调用excel文件,当保存时,如果点取消会报错,是怎么回事呢
我没用api编写,只是在需要调用excel的按钮里写了如下代码:PrivateSubImage6_Click()'调出excel,以便打印DimxlappAsExcel....
我没用api编写,只是在需要调用excel的按钮里写了如下代码:
Private Sub Image6_Click() '调出excel,以便打印
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlapp = CreateObject("excel.application")
Set xlbook = Excel.Workbooks.Open(App.Path & "\data\base.xls")
Set xlsheet = Excel.Worksheets(1)
xlapp.Visible = True
With xlsheet
.Cells(5, 3) = textname.Text '姓名
.Cells(5, 5) = "" '主险
.Cells(5, 7) = "" '附加险
Dim excel_i
For excel_i = 0 To 7
.Cells(excel_i + 7, 3) = Textresult1(excel_i) & " 元" '医疗费
.Cells(excel_i + 7, 4) = Textresult2(excel_i) & " 元" '扣除费
Next excel_i
.Cells(14, 5) = Textresult3(7) & " 元" '理算费
.Cells(15, 2) = Labelresult.Caption '理算公式
End With
xlbook.SaveAs "d:\计算表-" & textname.Text & ".xls"
End Sub
有时候保存好了,关闭excel后,发现任务管理器里还是有excel进程,
该怎么改进才好呢?
l00o00l你好,错误解决了,
但是
点击取消后,他问我要不要保存base.xls 就是我开始打开的那个。怎么能让它不显示呢?
我马上要上台讲解使用方法,这对我很重要。成功解决的话我会追加分的 展开
Private Sub Image6_Click() '调出excel,以便打印
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlapp = CreateObject("excel.application")
Set xlbook = Excel.Workbooks.Open(App.Path & "\data\base.xls")
Set xlsheet = Excel.Worksheets(1)
xlapp.Visible = True
With xlsheet
.Cells(5, 3) = textname.Text '姓名
.Cells(5, 5) = "" '主险
.Cells(5, 7) = "" '附加险
Dim excel_i
For excel_i = 0 To 7
.Cells(excel_i + 7, 3) = Textresult1(excel_i) & " 元" '医疗费
.Cells(excel_i + 7, 4) = Textresult2(excel_i) & " 元" '扣除费
Next excel_i
.Cells(14, 5) = Textresult3(7) & " 元" '理算费
.Cells(15, 2) = Labelresult.Caption '理算公式
End With
xlbook.SaveAs "d:\计算表-" & textname.Text & ".xls"
End Sub
有时候保存好了,关闭excel后,发现任务管理器里还是有excel进程,
该怎么改进才好呢?
l00o00l你好,错误解决了,
但是
点击取消后,他问我要不要保存base.xls 就是我开始打开的那个。怎么能让它不显示呢?
我马上要上台讲解使用方法,这对我很重要。成功解决的话我会追加分的 展开
展开全部
VB错误处理使用On Error语句。
我试着给你改了一下,你看是不是这样。
Private Sub Image6_Click() '调出excel,以便打印
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
'********************追加代码开始*********************
On Error GoTo Image6_Click_Err
'********************追加代码结束*********************
Set xlapp = CreateObject("excel.application")
Set xlbook = Excel.Workbooks.Open(App.path & "\data\base.xls")
Set xlsheet = Excel.Worksheets(1)
xlapp.Visible = True
With xlsheet
.Cells(5, 3) = textname.Text '姓名
.Cells(5, 5) = "" '主险
.Cells(5, 7) = "" '附加险
Dim excel_i
For excel_i = 0 To 7
.Cells(excel_i + 7, 3) = Textresult1(excel_i) & " 元" '医疗费
.Cells(excel_i + 7, 4) = Textresult2(excel_i) & " 元" '扣除费
Next excel_i
.Cells(14, 5) = Textresult3(7) & " 元" '理算费
.Cells(15, 2) = Labelresult.Caption '理算公式
End With
xlbook.SaveAs "d:\计算表-" & textname.Text & ".xls"
'********************追加代码开始*********************
Exit Sub
Image6_Click_Err:
If Not xlbook Is Nothing Then
xlbook.Close
Set xlbook = Nothing
End If
If Not xlapp Is Nothing Then
xlapp.Quit
Set xlapp = Nothing
End If
'********************追加代码结束*********************
End Sub
我试着给你改了一下,你看是不是这样。
Private Sub Image6_Click() '调出excel,以便打印
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
'********************追加代码开始*********************
On Error GoTo Image6_Click_Err
'********************追加代码结束*********************
Set xlapp = CreateObject("excel.application")
Set xlbook = Excel.Workbooks.Open(App.path & "\data\base.xls")
Set xlsheet = Excel.Worksheets(1)
xlapp.Visible = True
With xlsheet
.Cells(5, 3) = textname.Text '姓名
.Cells(5, 5) = "" '主险
.Cells(5, 7) = "" '附加险
Dim excel_i
For excel_i = 0 To 7
.Cells(excel_i + 7, 3) = Textresult1(excel_i) & " 元" '医疗费
.Cells(excel_i + 7, 4) = Textresult2(excel_i) & " 元" '扣除费
Next excel_i
.Cells(14, 5) = Textresult3(7) & " 元" '理算费
.Cells(15, 2) = Labelresult.Caption '理算公式
End With
xlbook.SaveAs "d:\计算表-" & textname.Text & ".xls"
'********************追加代码开始*********************
Exit Sub
Image6_Click_Err:
If Not xlbook Is Nothing Then
xlbook.Close
Set xlbook = Nothing
End If
If Not xlapp Is Nothing Then
xlapp.Quit
Set xlapp = Nothing
End If
'********************追加代码结束*********************
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-11-05
展开全部
Excel.Application对象创建后,系统将启动Excel进程,在程序运行完毕后,必须让Excel.Application对象Quit(退出),否则,系统进程将无法消除。如同你的应用程序,在程序结束时要End掉一样。如果Excel.Application不Quit,就如同你的程序未End而仅仅Hide,程序实际尚未结束,只是未在任务栏显示。另外,编程要形成一个良好的习惯,创建的对象要卸载:Set obj = Nothing以释放内存。
鉴于此,在你的程序结束前,要:
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
鉴于此,在你的程序结束前,要:
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
xlBook.Close
xlApp.Quit '关闭Excel
Set xlBook = Nothing
Set xlApp = Nothing
xlApp.Quit '关闭Excel
Set xlBook = Nothing
Set xlApp = Nothing
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在End Sub前面加上下面的几行代码:
xlbook.Close
xlapp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
xlbook.Close
xlapp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询