Excel调用的棘手问题,急求解!

Hermit007 Hermit007 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在VB中做了一个EXCEL的调用,用完后关闭Excel,也删除了我创建的临时文件"c:\temp3.xls",但在进程中还有一个Excel,如果此进程不关闭,在下次调用中就会出错,能告诉我如何安全正常的关闭它吗?谢谢!
急!


'===========================================================
Option Explicit
Private Sub Command1_Click()

Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlBook = xlApp.Workbooks.Add '创建一个新表
Set xlSheet = xlApp.Workbooks(1).Worksheets(1)

'在新建的EXCEL中(如果只写了一些临时数据它可正常关闭)
With xlApp.ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.google.com", Destination _
:=Range("A1"))
.Name = "www.google.com"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
' .WebSelectionType = xlAllTables '选择目标所在WEB网页的具体位置:表格
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = False
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.Refresh BackgroundQuery:=False
End With


'使用完毕
xlBook.SaveAs FileName:="C:\temp3.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

Set xlSheet = Nothing
xlBook.RunAutoMacros (xlAutoClose)
xlBook.Close (True) '关闭工作簿
Set xlBook = Nothing
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象

Kill "c:\temp3.xls"
End Sub
...全文

相关推荐

评论 14
收藏 已收藏
分享
返回顶部
Hermit007
VB基础类 7,788 成员 197,583 内容