vba access - 检查excel文件是否已打开

时间:2015-05-20 23:21:55

标签: vba ms-access access-vba ms-access-2010

使用VBA Access,我尝试编辑以下代码。这样它首先检查excel文件是否打开,如果已打开等待文件关闭然后恢复代码?这需要为每个文件1,2,3。

完成
  1. 检查excel文件是否已打开
  2. 如果打开等待(暂停)直到关闭然后恢复代码(刷新表,保存,关闭)
  3. 重复下一个文件的处理。

      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
    

1 个答案:

答案 0 :(得分:0)

在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