我写的一个把表格中的数据导出到execl中的函数,能导出成功,但就是导出一次后,就要关闭一次应用程序,然后再次进入程序才可以再导出到execl

suinx 2003-08-22 09:24:15
我写的一个把表格中的数据导出到execl中的函数,能导出成功,但就是导出一次后,就要关闭一次应用程序,然后再次进入程序才可以再导出到execl,否则就导不出来.我想是对象没有完全释放的原因?

那位有这方面的经验,请指点一下,谢谢!
代码如下:
Private Function GridSaveAsExcel2(vsGrid As VSFlexGrid, Optional strFile As String, Optional strTitle As String) As Boolean
On Error GoTo errHandle

Dim i As Integer, j As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook 'Object
Dim xlSheet As Excel.Worksheet ' Object

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


Set xlBook = xlApp.Workbooks().Add

Set xlSheet = xlBook.Worksheets("sheet1")


xlSheet.Cells(1, 1).Value = strTitle


'跨列居中
Range("A1:G1").Select
With Selection
.HorizontalAlignment = xlCenter
' .VerticalAlignment = xlBottom
' .WrapText = False
' .Orientation = 0
' .AddIndent = False
' .ShrinkToFit = False
.MergeCells = True
End With
Selection.Merge

With vsGrid
For i = 0 To .Rows - 1
For j = 0 To .Cols - 1
If .ColFormat(j) = "" Then
If Len(Trim(.TextMatrix(i, j))) >= 8 And j < 3 Then
xlSheet.Cells(i + 2, j + 1) = "'" & Trim(.TextMatrix(i, j))
Else
xlSheet.Cells(i + 2, j + 1) = Trim(.TextMatrix(i, j))
End If
Else
xlSheet.Cells(i + 2, j + 1) = Trim(.TextMatrix(i, j))
End If
Next j
Next i
End With

ActiveWorkbook.SaveAs FileName:=strFile, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False


' 关闭工作表。
xlBook.Close
'用 Quit 方法关闭 Microsoft Excel
xlApp.Quit
'释放对象
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing


GridSaveAsExcel2 = True
Exit Function

errHandle:
GridSaveAsExcel2 = False

End Function
...全文
104 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
WQ771211 2003-08-24
  • 打赏
  • 举报
回复
这种方法效率太低
office2000支持HTML
不如写成HTML格式,在存放为xxxx.xls就行了
qifanghcy 2003-08-23
  • 打赏
  • 举报
回复
在Form_Unload()事件中:
IF typename(xlapp)<>"Nothing" THEN
xlapp.quit
set xlapp=nothing
END IF
TechnoFantasy 2003-08-22
  • 打赏
  • 举报
回复
你代码中设置了On Error GoTo ,那么你调试一下,看看xlApp.Quit这个执行到了没有。
diyee 2003-08-22
  • 打赏
  • 举报
回复
如下:
errHandle:
' 关闭工作表。
xlBook.Close
'用 Quit 方法关闭 Microsoft Excel
xlApp.Quit
GridSaveAsExcel2 = False

End Function
suinx 2003-08-22
  • 打赏
  • 举报
回复
xlApp.Quit已经执行了,但按Ctrl+Alt+Del,可看到列表中Excel进程还在没有退出.

怎么回事啊?
xingnup 2003-08-22
  • 打赏
  • 举报
回复
调试一下看.
若想知Excel有没有完全退出,按Ctrl+Alt+Del,看列表中有没有Excel.若有则未完成退出.

2,461

社区成员

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

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