我正在努力弄清楚在对它进行多次操作后是否应该关闭Excel。如果我已经打开Excel(A.xls,D.xls)并启动我的Word宏,它从活动文档获取值并将其转储到B.xls中,我希望我的宏关闭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实现这一目标?
答案 0 :(得分:1)
这将检查工作簿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对象库&#39;参考文献
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
答案 1 :(得分:0)
我找到了一个解决方案,也许并不优雅,但它确实有效。我只是检查窗口名称以查看它是否像Excel一样被调用,请参阅此post。
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
答案 2 :(得分:0)
它不应该关闭其他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