Excel.Application对象.Quit使EXCEL.EXE运行
我在Windows 10的MS Access 2013中工作,我只是试图写一个Sub,打开磁盘上的Excel文件,更改列格式和一些列,保存并退出。 子运行和按预期方式,但问题是,甚至在.Quit命令后,“EXCEL.EXE”保持运行,随后调用该子将导致运行时错误。 如果我运行子后closuresAccess,“EXCEL.EXE”消失在任务pipe理器上,但不是如果我做“压缩和修复”的数据库。
我做了另一个小组,只是打开Excel文件在磁盘上,并将所有列的宽度“自动宽度”,然后closures,这工作正常,不会离开“EXCEL.EXE”运行。
我究竟做错了什么? 是否可以在VBA中确保“EXCEL.EXE”正确退出?
我尝试了很多不同的行顺序Dims和设置为Nothing,工作簿closures,等等等等。另外,我在这里和其他网站上search如何解决这个问题,但2小时后,唯一的build议,我见过的VBA使用像ThisWorkbook.Saved = True,但我试过之前和之后。退出不起作用。 除此之外,我只findVB.NET或其他环境的解决scheme,我没有使用,现在旁边什么也没有。
感谢您阅读此。
代码如下:
Sub changeXLcolumnFormatting() Dim XL As Object Set XL = CreateObject("Excel.Application") XL.Visible = False XL.DisplayAlerts = False XL.Workbooks.Open "C:\Users\640344\Desktop\rawDataTest.XLSX" Dim sht As Worksheet With XL Set sht = ActiveWorkbook.Worksheets(1) Dim rng As Range Dim i As Integer, j As Integer field_names = Split("datasistema|Data de Registo|Data Registo CMVM", "|") sht.Select end_of_table = sht.UsedRange.Columns.Count For j = 0 To UBound(field_names) For i = 1 To end_of_table Set rng = sht.Cells(1, i) If InStr(rng.Text, field_names(j)) > 0 Then sht.Columns(i).NumberFormat = "yyyy-mm-dd HH:MM:ss" End If Next i Next j End With Set rng = Nothing Set sht = Nothing XL.ActiveWorkbook.Close (True) XL.Quit Set XL = Nothing End Sub
声明并使用特定的工作簿对象 – 就像您对工作表和范围所做的一样,如下所示:
Dim xls As Excel.Application Dim wkb As Excel.Workbook Dim wks As Excel.Worksheet Dim rng As Range Set xls = New Excel.Application Set wkb = xls.Workbooks.Open("c:\test\workbook1.xlsx") Set wks = wkb.Worksheets(1) Set rng = wks.Range(<something>) ' Do stuff. wks.Name = "My New Name" With rng ' Do more. End With wkb.Close True Set rng = Nothing Set wks = Nothing Set wkb = Nothing xls.Quit Set xls = Nothing
此外,不要使用select,这只是可见的使用。 定义范围。
Cinetyk的编辑:
使用@Gustav的指示,那些我想要的并且解决问题的代码是:
Sub changeXLcolumnFormatting() Dim XL As Excel.Application Dim sht As Excel.Worksheet Dim wkb As Excel.Workbook Dim rng As Range Set XL = New Excel.Application XL.Visible = False XL.DisplayAlerts = False Set wkb = XL.Workbooks.Open("C:\Users\640344\Desktop\rawDataTest.XLSX") Set sht = wkb.Worksheets(1) Dim i As Integer, j As Integer field_names = Split("datasistema|Data de Registo|Data Registo CMVM", "|") end_of_table = sht.UsedRange.Columns.Count For j = 0 To UBound(field_names) For i = 1 To end_of_table Set rng = sht.Cells(1, i) If InStr(rng.Text, field_names(j)) > 0 Then sht.Columns(i).NumberFormat = "yyyy-mm-dd HH:MM:ss" End If Next i Next j wkb.Close (True) Set rng = Nothing Set sht = Nothing XL.Quit Set XL = Nothing End Sub
这是一个奇特的方式来解决这个问题 – 使用with new Excel.Application
:
Option Compare Database Option Explicit Public Sub TestMe() Dim wkb As Object Dim wks As Object With New Excel.Application Set wkb = .Workbooks.Open("C:\Users\vityata\Desktop\myTest.xlsx") Set wks = wkb.Worksheets(1) wkb.Close True Set wks = Nothing Set wkb = Nothing .Quit End With End Sub
在C#中这是Using
VBA的标准,很less有人使用它 – 我从来没有见过它的生产代码。
Cinetyk的编辑:
使用Vityata的指示,按照我的意图工作的代码是:
Option Compare Database Option Explicit Public Sub changeXLcolumnFormattingV2() Dim sht As Object Dim wkb As Object With New Excel.Application .Visible = False .DisplayAlerts = False Set wkb = .Workbooks.Open("C:\Users\640344\Desktop\rawDataTESTING.XLSX") Set wks = wkb.Worksheets(1) field_names = Split("datasistema|Data de Registo|Data Registo CMVM", "|") end_of_table = wks.UsedRange.Columns.Count For j = 0 To UBound(field_names) For i = 1 To end_of_table Set rng = wks.Cells(1, i) If InStr(rng.Text, field_names(j)) > 0 Then wks.Columns(i).NumberFormat = "yyyy-mm-dd HH:MM:ss" End If Next i Next j wkb.Close True Set wks = Nothing Set wkb = Nothing .Quit End With End Sub