[英]Check if Excel is open from Word
我正在努力找出是否应该对它执行几次操作后关闭Excel。 如果我已经打开Excel(A.xls,D.xls)并启动要从活动文档中获取值并将其转储到B.xls中的Word宏,我希望我的宏关闭B.xls,但将A和C保持打开状态。 但是,如果我没有打开A和C,我希望宏在完成运行后完全关闭Excel。
我尝试了几种方法来成功实现这一目标:
Dim oXLApp As Object
' Get excel object
closeExcelMy = FileHandling.setExcelObject(oXLApp)
'*********************************************************
'********* define if we need to close excel after sub is done
'***********************************************************
Function setExcelObject(ByRef oXLApp As Object) As Boolean
On Error GoTo notOpen
setExcelObject = False
Set oXLApp = GetObject(, "Excel.Application")
' On Error GoTo 0
' If oXLApp Is Nothing Then
' Set oXLApp = CreateObject("Excel.Application")
' setExcelObject = True
' End If
notOpen:
Set oXLApp = CreateObject("Excel.Application")
setExcelObject = True
'~~> Hide Excel
'oXLApp.Visible = True
' If oXLApp.Workbooks.Count < 1 Then
' setExcelObject = True
' End If
End Function
如果要关闭Excel,我希望函数返回true
,否则返回false
。
如何在Word中从VBA实现此目的?
这将检查工作簿A.xls
和C.xls
是否都打开。
如果两个工作簿都没有打开,它将关闭整个Excel应用程序。 您应该能够将其应用到代码中并获得有效的解决方案。
Option Explicit
Function areWorkbooksOpen() As Boolean
Dim wbA As Workbook, wbC As Workbook
On Error Resume Next ' Turn off error handling
Set wbA = Workbooks("A.xls")
Set wbC = Workbooks("C.xls")
On Error GoTo 0
If wbA Is Nothing Or wbC Is Nothing Then
areWorkbooksOpen = False
Else
areWorkbooksOpen = True
End If
End Function
Sub mainCode()
' Your code
If Not areWorkbooksOpen Then
Excel.Application.Quit ' Close entire session of excel
End If
End Sub
编辑
如果尚未找到excel应用程序,则需要这样做。 下面考虑了找到excel应用程序的问题。
您还需要在“引用”中启用“ Microsoft Excel 14.0对象库”
Option Explicit
Function areWorkbooksOpen() As Boolean
Dim excelApp As Excel.Application, wb As Workbook
Dim wbA As Workbook, wbC As Workbook
Set excelApp = GetObject(, "Excel.Application")
For Each wb In excelApp.Workbooks
If wb.Name = "A.xls" Then
Set wbA = wb
ElseIf wb.Name = "C.xls" Then
Set wbC = wb
End If
Next wb
If wbA Is Nothing Or wbC Is Nothing Then
areWorkbooksOpen = False
Else
areWorkbooksOpen = True
End If
Set excelApp = Nothing
End Function
Sub mainCode()
' Your code
If Not areWorkbooksOpen Then
Excel.Application.Quit
End If
End Sub
我找到了一个解决方案,也许不是很优雅,但它可行。 我只是检查窗口名称以查看它是否被称为Excel之类的东西,请参阅这篇文章 。
Option Explicit
Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Sub test()
Dim closeExcel As Boolean
closeExcel = ListWins("*Excel*")
End Sub
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
它不应关闭其他Excel文件。
我使用了以下内容:
Sub SomeMacro()
Dim ExcelProgram As Object
Dim ExcelFile As Object
Dim EventData As Object
'Here you would be using your script to develop/determine the file path
ExcelFilePath = "C:\ Some File Path.xls"
'Starting Excel
Set ExcelProgram = CreateObject("Excel.Application")
'Not allowing it to be visible
ExcelProgram.Application.Visible = False
'Opening the desired Excel File
Set ExcelFile = ExcelProgram.Application.Workbooks.Open(ExcelFilePath)
'Here you would execute some script for the Excel Sheet
Somecode here
'Quiting the Excel Application
ExcelProgram.Quit
'clean up Objects for next use
Set ExcelProgram = Nothing
Set ExcelFile = Nothing
End Sub
并且它不会关闭任何其他打开的Excel文档,因为您关闭了创建的ExcelProgram.Quit
而不是Excel.Application.Quit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.