Vérifiez si Excel est ouvert à partir de Word

Je suis en difficulté pour comprendre si je devrais fermer Excel après avoir effectué plusieurs opérations. Si j'ai déjà Excel ouvert (A.xls, D.xls) et démarrer ma macro Word qui prend de la valeur du document actif et la détruit dans B.xls, je veux que ma macro ferme B.xls mais laisse A et C ouverts. Cependant, si je n'ai pas A et C ouverts, je souhaite que ma macro ferme complètement Excel complètement après sa mise en route.

J'ai essayé plusieurs façons d'y parvenir sans réussir:

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 

Je veux que ma fonction returnne true si je peux fermer Excel autrement false .

Comment puis-je get cela à partir de VBA dans Word?

Cela vérifiera si les classurs A.xls et C.xls sont ouverts.

Si l'un des classurs n'est pas ouvert, il va fermer l'application Excel complète. Vous devriez pouvoir travailler dans votre code et get une solution de travail.

 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 

MODIFIER

Si vous n'avez pas déjà trouvé l'application Excel, vous devrez le faire. Les éléments ci-dessous permettent de considérer la solution excel.

Vous devrez également activer 'Microsoft Excel 14.0 Object Library' dans les references

 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 

J'ai trouvé une solution, peut-être pas élégante, mais ça marche. Je vérifie simplement les noms des windows pour voir si c'est appelé quelque chose comme Excel, voir cette publication .

 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 Ssortingng, ByVal lpWindowName As Ssortingng) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As Ssortingng, ByVal nMaxCount As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpSsortingng As Ssortingng, 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(vbNullSsortingng, vbNullSsortingng) While hWndThis Dim sTitle As Ssortingng, sClass As Ssortingng 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 

Il ne devrait pas fermer les autres files Excel.

J'ai utilisé ce qui suit:

 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 

et il ne ferme aucun autre document Excel ouvert, car vous fermez ExcelProgram.Quit créé et non Excel.Application.Quit