我试图找出excel是否正在运行,因为在我创建了excel文件后,我需要知道是否应该关闭excel。
我的方法对我来说很好,但它在另一个用户上失败了,我不知道它为什么会失败。我的赌注是while循环永远运行。那么我应该添加一个计数器来避免这种情况,还是有更好的方法来确定excel是否正在运行?
Function ListWins(Optional title = "*", Optional Class = "*") As Boolean
Dim hWndThis As Long
ListWins = True
hWndThis = FindWindow(vbNullString, vbNullString)
While hWndThis
Dim sTitle As String, sClass As String
sTitle = Space$(255)
sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
sClass = Space$(255)
sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
If sTitle Like title And sClass Like Class Then
ListWins = False
Debug.Print sTitle, sClass
End If
hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
Wend
End Function
答案 0 :(得分:0)
所以这是适用于我的新功能:
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If
Function ExcelOpen() As Boolean
ExcelOpen = FindWindow("XLMAIN", vbNullString)
End Function