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:
我在Outlook VBA中为自己编写了一种本土的票务系统,我使用Excel来存储所有持久数据。我有一个用Outlook编写的函数来从.csv获取一些数据并将其返回。这一切都运行正常,但在关闭工作簿后,退出应用程序,并将应用程序设置为空,我仍然运行Excel进程!这是我的代码:
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 ...我该怎么做才能解决这个问题?
1 个解决方案
#1
3
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 lineRange("a1").Value = Range("a1").Value + 1
leave the xl instance open
这里提出的问题正是你所拥有的。此行范围(“a1”)。值=范围(“a1”)。值+ 1使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
#1
3
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 lineRange("a1").Value = Range("a1").Value + 1
leave the xl instance open
这里提出的问题正是你所拥有的。此行范围(“a1”)。值=范围(“a1”)。值+ 1使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