Excel.Application Object .Quit leaves EXCEL.EXE running

后端 未结 2 1752
不思量自难忘°
不思量自难忘° 2020-12-20 02:00

I\'m working within MS Access 2013 on Windows 10 and I\'m simply trying to write a Sub that opens an Excel file on disk, changes the column formatting and some columns, save

2条回答
  •  既然无缘
    2020-12-20 02:40

    Here is a fancy way to solve that problem - using 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
    

    In C# this is a standard with Using, in VBA very few people use it - I have never seen it production code.


    Cinetyk's EDIT:

    Using Vityata's indications, the code that works as I intended is:

    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
    

提交回复
热议问题