简体   繁体   中英

vba access - Check if excel file is open

Using VBA Access, I'm trying to edit the below code. So that it first checks if an excel file is open , if already open wait till file is closed then resume code? this needs to be done for each file 1,2,3.

  1. Check if excel file is open
  2. If open wait(Pause) till closed then resume code (Refresh table ,save, close)
  3. Repeat process for the next file.

      Function RefreshExcelTables() Dim ExcelApp As Object Set ExcelApp = CreateObject("Excel.Application") ExcelApp.workbooks.Open "c:\\test\\Test_Sheet1.xlsb" ExcelApp.ActiveWorkbook.refreshall ExcelApp.ActiveWorkbook.Save ExcelApp.ActiveWindow.Close ExcelApp.workbooks.Open "c:\\test\\Test_Sheet2.xlsb" ExcelApp.ActiveWorkbook.refreshall ExcelApp.ActiveWorkbook.Save ExcelApp.ActiveWindow.Close ExcelApp.workbooks.Open "c:\\test\\Test_Sheet3.xlsb" ExcelApp.ActiveWorkbook.refreshall ExcelApp.ActiveWorkbook.Save ExcelApp.ActiveWindow.Close Set ExcelApp = Nothing End Function 

tested in Outlook

Sub Test()

Dim ExcelApp As Object
Dim X, X1

X = Array("c:\test\Test_Sheet1.xlsb", "c:\test\Test_Sheet2.xlsb")

For Each X1 In X
    On Error Resume Next
    Set ExcelApp = GetObject(X1).Application
    On Error GoTo 0

    If Not ExcelApp Is Nothing Then
        With ExcelApp.Workbooks(Right$(X1, Len(X1) - InStrRev(X1, "\")))
            .RefreshAll
            .Save
            .Close
    End With
    Set ExcelApp = Nothing
    End If

Next X1
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM