这段写EXCEL报表的程序运行时出现异常!

lcj49997 2005-10-04 09:10:07
一个产生日报的函数如下,在有些机器上调用时会出现异常,大致是:Excel应用程序关闭...在创建错误日志。报表还是正确生成了,就是每次调用的时候弹出一个异常,很不方便的,请高手帮我看一下是不是代码有问题。
Private Sub input_ribao(strdate As String)

Dim strsource1, strsource, strdestination, strdestination1, strdestination2, strdestination3 As String

Dim xlapp As Excel.Application
Dim xlbook1 As Excel.Workbook
Dim xlsheet1 As Excel.Worksheet

Dim xlbook2 As Excel.Workbook
Dim xlsheet2 As Excel.Worksheet
Dim xlbook3 As Excel.Workbook
Dim xlsheet3 As Excel.Worksheet

Set xlapp = New Excel.Application
Set xlapp = CreateObject("Excel.Application")

strsource1 = "d:\template\template1.xls"
strdestination1 = "d:\baobiaoku\banbao_beiyong\" + Format(strdate, "yyyy_mm_dd") + "_00" + ".xls"
If Not fileexists(strdestination1) Then
FileCopy strsource1, strdestination1
End If
Set xlbook1 = xlapp.Workbooks.Open(strdestination1)
Set xlsheet1 = xlbook1.Sheets(1)

strdestination2 = "d:\baobiaoku\banbao_beiyong\" + Format(strdate, "yyyy_mm_dd") + "_08" + ".xls"
If Not fileexists(strdestination2) Then
FileCopy strsource1, strdestination2
End If
Set xlbook2 = xlapp.Workbooks.Open(strdestination2)
Set xlsheet2 = xlbook2.Sheets(1)

strdestination3 = "d:\baobiaoku\banbao_beiyong\" + Format(strdate, "yyyy_mm_dd") + "_16" + ".xls"
If Not fileexists(strdestination3) Then
FileCopy strsource1, strdestination3
End If
Set xlbook3 = xlapp.Workbooks.Open(strdestination3)
Set xlsheet3 = xlbook3.Sheets(1)


strdestination = "d:\baobiaoku\ribao\ribao.xls"
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
If Not fileexists(strdestination) Then
strsource1 = "d:\template\template2.xls"
FileCopy strsource1, strdestination
End If
Set xlbook = xlapp.Workbooks.Open(strdestination)
Set xlsheet = xlbook.Sheets(1)

Dim L As Single
L = 7
xlsheet.Cells(L, 5) = xlsheet1.Cells(25 - L, 4) + xlsheet2.Cells(25 - L, 4) + xlsheet3.Cells(25 - L, 4)
xlsheet.Cells(L, 7) = xlsheet1.Cells(25 - L, 7) + xlsheet2.Cells(25 - L, 7) + xlsheet3.Cells(25 - L, 7)
xlsheet.Cells(L, 9) = xlsheet1.Cells(25 - L, 10) + xlsheet2.Cells(25 - L, 10) + xlsheet3.Cells(25 - L, 10)
xlsheet.Cells(L, 11) = xlsheet1.Cells(25 - L, 13) + xlsheet2.Cells(25 - L, 13) + xlsheet3.Cells(25 - L, 13)
xlsheet.Cells(L, 6) = xlsheet1.Cells(26 - L, 4) + xlsheet2.Cells(26 - L, 4) + xlsheet3.Cells(26 - L, 4)
xlsheet.Cells(L + 1, 6) = xlsheet1.Cells(27 - L, 4) + xlsheet2.Cells(27 - L, 4) + xlsheet3.Cells(27 - L, 4)
xlsheet.Cells(L + 2, 6) = xlsheet1.Cells(28 - L, 4) + xlsheet2.Cells(28 - L, 4) + xlsheet3.Cells(28 - L, 4)
xlsheet.Cells(L + 3, 6) = xlsheet1.Cells(29 - L, 4) + xlsheet2.Cells(29 - L, 4) + xlsheet3.Cells(29 - L, 4)
xlsheet.Cells(L + 4, 6) = xlsheet1.Cells(30 - L, 4) + xlsheet2.Cells(30 - L, 4) + xlsheet3.Cells(30 - L, 4)
xlsheet.Cells(L, 8) = xlsheet1.Cells(26 - L, 7) + xlsheet2.Cells(26 - L, 7) + xlsheet3.Cells(26 - L, 7)
xlsheet.Cells(L + 2, 8) = xlsheet1.Cells(28 - L, 7) + xlsheet2.Cells(28 - L, 7) + xlsheet3.Cells(28 - L, 7)
xlsheet.Cells(L + 1, 8) = xlsheet1.Cells(27 - L, 7) + xlsheet2.Cells(27 - L, 7) + xlsheet3.Cells(27 - L, 7)
xlsheet.Cells(L + 3, 8) = xlsheet1.Cells(29 - L, 7) + xlsheet2.Cells(29 - L, 7) + xlsheet3.Cells(29 - L, 7)
xlsheet.Cells(L + 4, 8) = xlsheet1.Cells(30 - L, 7) + xlsheet2.Cells(30 - L, 7) + xlsheet3.Cells(30 - L, 7)
xlsheet.Cells(L, 10) = xlsheet1.Cells(26 - L, 10) + xlsheet2.Cells(26 - L, 10) + xlsheet3.Cells(26 - L, 10)
xlsheet.Cells(L + 1, 10) = xlsheet1.Cells(27 - L, 10) + xlsheet2.Cells(27 - L, 10) + xlsheet3.Cells(27 - L, 10)
xlsheet.Cells(L + 2, 10) = xlsheet1.Cells(28 - L, 10) + xlsheet2.Cells(28 - L, 10) + xlsheet3.Cells(28 - L, 10)
xlsheet.Cells(L + 3, 10) = xlsheet1.Cells(29 - L, 10) + xlsheet2.Cells(29 - L, 10) + xlsheet3.Cells(29 - L, 10)
xlsheet.Cells(L + 4, 10) = xlsheet1.Cells(30 - L, 10) + xlsheet2.Cells(30 - L, 10) + xlsheet3.Cells(30 - L, 10)
xlsheet.Cells(L, 12) = xlsheet1.Cells(26 - L, 13) + xlsheet2.Cells(26 - L, 13) + xlsheet3.Cells(26 - L, 13)
xlsheet.Cells(L + 1, 12) = xlsheet1.Cells(27 - L, 13) + xlsheet2.Cells(27 - L, 13) + xlsheet3.Cells(27 - L, 13)
xlsheet.Cells(L + 2, 12) = xlsheet1.Cells(28 - L, 13) + xlsheet2.Cells(28 - L, 13) + xlsheet3.Cells(28 - L, 13)
xlsheet.Cells(L + 3, 12) = xlsheet1.Cells(29 - L, 13) + xlsheet2.Cells(29 - L, 13) + xlsheet3.Cells(29 - L, 13)
xlsheet.Cells(L + 4, 12) = xlsheet1.Cells(30 - L, 13) + xlsheet2.Cells(30 - L, 13) + xlsheet3.Cells(30 - L, 13)
xlbook.SaveAs ("d:\baobiaoku\ribao\" + Format(Date, "yyyy_mm_dd") + ".xls")

xlapp.Quit



Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
Set xlbook1 = Nothing
Set xlsheet1 = Nothing
Set xlbook2 = Nothing
Set xlsheet2 = Nothing
Set xlbook3 = Nothing
Set xlsheet3 = Nothing

End Sub
...全文
191 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcj49997 2005-10-15
  • 打赏
  • 举报
回复
谢谢两位高手的答复!
gowowo 2005-10-06
  • 打赏
  • 举报
回复
我不能再现你的问题只能估计原因为:
(1)重复创建 Excel.Application对象 (2)Excel.Application对象不可视.
解决办法:
(1) 删除 “ Set xlapp = New Excel.Application ” 行
(2) 把 “ Set xlapp = CreateObject("Excel.Application") ” 改为
“ Set xlapp = CreateObject("Excel.Application"):xlapp.Application.Visible = True ”

faysky2 2005-10-06
  • 打赏
  • 举报
回复

你把捕捉异常的语句去掉,看错误出现在哪一语句上
内容概要:本文详细介绍了利用Simulink进行变压器开路试验的电路连接配置与仿真实现方法,重点在于通过仿真手段还原实际电力系统中变压器在空载条件下的电气特性,从而深入理解其工作原理与性能表现。文章作为电力系统仿真系列研究的一部分,系统阐述了从电路模型搭建、参数设定、仿真运行到结果分析的完整流程,突出展示了MATLAB/Simulink在电力设备建模与教学科研中的强大功能与应用价值。; 适合人群:具备电力系统基础知识,熟悉MATLAB/Simulink仿真环境,从事电气工程、自动化及相关领域的研发人员,以及高年级本科生和研究生。; 使用场景及目标:①掌握变压器开路试验的基本原理与Simulink仿真建模的具体步骤;②通过仿真实验深入理解空载电流、铁芯损耗及励磁特性等关键参数的物理意义;③为后续开展变压器短路试验、暂态过程分析以及其他电力设备的仿真研究奠定理论与实践基础。; 阅读建议:建议结合Simulink软件动手实践,逐步构建并调试电路模型,重点关注各元件参数的设置方法与测量模块的应用技巧,同时推荐参考文中提及的其他相关仿真案例进行拓展学习,以全面提升对电力系统仿真实践的整体认知与操作能力。

2,506

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧