Excel应用程序未从Outlook VBA功能关闭 [英] Excel application not closing from Outlook VBA function

查看:70
本文介绍了Excel应用程序未从Outlook VBA功能关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Outlook VBA中为自己编写一种自行开发的票务系统,并且正在使用Excel存储所有持久性数据.我有一个用Outlook编写的功能,可以从.csv获取一些数据并将其返回.一切正常,但是关闭工作簿,退出应用程序,并将应用程序设置为空后,我仍然有一个Excel进程正在运行!这是我的代码:

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. I have a function written in Outlook to get some data from the .csv and return it. 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! 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

我在这里做错什么了吗?我的问题类似于此处,但是我已禁用DisplayAlerts ...我该怎么做才能解决此问题?

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?

推荐答案

尝试从 xl_doesnt_quit

这里出现的问题正是您所遇到的.这条线
Range("a1").Value = Range("a1").Value +1
让xl实例保持打开状态

The problem presented here is exactly what you have. This line
Range("a1").Value = Range("a1").Value + 1
leave the xl instance open

此问题的最常见原因是对自动化应用程序的全局"引用.不幸的是,在某些情况下,有可能直接引用自动化对象的实体(属性/方法/对象).该引用实际上是调用应用程序的全局引用.因此,只要调用程序处于活动状态,参考就保持不变.因此,在调用者处于活动状态时,操作系统将不会结束自动化应用程序.

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).

请更改您的适应路径.

代码

  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

这篇关于Excel应用程序未从Outlook VBA功能关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆