简体   繁体   English

Excel 应用程序未从 Outlook VBA 功能关闭

[英]Excel application not closing from Outlook VBA function

I'm writing a sort of homegrown ticketing system for myself in Outlook VBA, and I'm using Excel to store all the persistant data.我正在为自己在 Outlook VBA 中编写一种本土的票务系统,并且我正在使用 Excel 来存储所有持久性数据。 I have a function written in Outlook to get some data from the .csv and return it.我有一个用 Outlook 编写的函数用于从 .csv 获取一些数据并返回它。 This is all working fine, but after I close the workbook, quit the application, and set the app to nothing I still have an Excel process running!这一切正常,但在我关闭工作簿后,退出应用程序,并将应用程序设置为空,我仍然有一个 Excel 进程在运行! Here is my code:这是我的代码:

Private Function GetNewTicketNumber() As Integer
    Dim xlApp As Excel.Application
    Set xlApp = New Excel.Application
    With xlApp
        .Visible = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    Dim FileStr As String
    Dim NumberBook As Workbook
    Dim TheRange As Range
    FileStr = "C:\OMGITSAPATH.csv"
    Set NumberBook = Workbooks.Open(FileStr)
    Set TheRange = NumberBook.Worksheets(1).Range("A1")
    GetNewTicketNumber = TheRange.Value
    TheRange.Value = TheRange.Value + 1
    NumberBook.Save
    NumberBook.Close
    xlApp.Quit
    With xlApp
        .Visible = True
        .EnableEvents = True
        .DisplayAlerts = True
    End With

    Set xlApp = Nothing
End Function

Is there something that I'm doing wrong here?我在这里做错了什么吗? My problem is similar to the one here , but I have disabled DisplayAlerts... What can I do to fix this problem?我的问题与此处的问题类似,但我已禁用 DisplayAlerts...我该如何解决此问题?

Try fully qualifying your references to Excel, from xl_doesnt_quit尝试从xl_doesnt_quit完全限定对 Excel 的引用

The problem presented here is exactly what you have.此处提出的问题正是您所面临的问题。 This line这条线
Range("a1").Value = Range("a1").Value + 1
leave the xl instance open让 xl 实例保持打开状态

The most common cause of the problem is a 'global' reference to the automated application.问题的最常见原因是对自动化应用程序的“全局”引用。 Unfortunately, under some circumstances it is possible to directly refer to an entity (property/method/object) of the automated object.不幸的是,在某些情况下,可以直接引用自动化对象的实体(属性/方法/对象)。 This reference effectively is global to the calling application.该引用实际上对调用应用程序是全局的。 Hence, the reference remains in place as long as the calling program is active.因此,只要调用程序处于活动状态,引用就会保持原位。 Consequently, the operating system will not end the automated application while the caller is active.因此,当调用者处于活动状态时,操作系统不会结束自动应用程序。

Re-cut code below (which also uses late binding - which rules out the unqualified possibility).重新剪切下面的代码(它也使用后期绑定 - 这排除了不合格的可能性)。

Pls change you path to suit.请改变你的道路以适应。

code代码

  Private Function GetNewTicketNumber() As Long
    Dim xlApp As Object
    Dim objWB As Object
    Dim objWs As Object
    Dim FileStr As String

    FileStr = "C:\temp\test.xlsx"

    Set xlApp = CreateObject("excel.application")

    With xlApp
        .EnableEvents = False
        .DisplayAlerts = False
    End With

    Set objWB = xlApp.Workbooks.Open(FileStr)
    Set objWs = objWB.Sheets(1)
    GetNewTicketNumber = objWs.Range("A1")
    objWs.Range("A1") = objWs.Range("A1") + 1

    objWB.Save
    objWB.Close

    Set objWB = Nothing
    xlApp.Quit
    Set xlApp = Nothing
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM