在恢复之前需要访问VBA等待Excel进程完成

我有一个Access数据库有一个VBA函数触发Excel工作簿中的刷新。 Excel工作簿由一个打击Oracle OBIEE的.iqy文件提供。

根据networkingstream量,刷新Excel工作簿所用的时间会有所不同。

我在VBA模块的其他地方包括了一个pause(seconds)function,这个function在VBAfunction中延迟了进一步的执行,但问题是:当networking通信速度很快时,我总是延迟执行VBA,因为Excel刷新速度相对较快。 当networkingstream量较慢时,指定的pause()pause()秒数不够,程序崩溃。

有没有办法让VBA跟踪在Excel中的刷新过程,并在过程完成时恢复正确执行?

我的代码到目前为止如下:

 Function import_tblFromIQY() Dim Path As String Path = CurrentProject.Path & "\" Dim rbFileName As String rbFileName = "dataPipeline.xls" ' If table tblFromIQY exists, then delete. If TableExists("tblFromIQY") = True Then DoCmd.DeleteObject acTable, "tblFromIQY" Debug.Print ">>> Deleted table tblFromIQY" End If ' Refresh linked table Dim appexcel As Excel.Application Dim wr As Excel.Workbook Dim wrksheet As Excel.Worksheet Set appexcel = CreateObject("Excel.Application") Set wr = appexcel.workbooks.Open(Path & rbFileName) Set wrksheet = appexcel.worksheets("Main Worksheet") wr.RefreshAll Pause (420) ' pause for 7 mins. this is approx how long it takes for the file to refresh. wrksheet.Rows("1:1").Select wrksheet.Rows("1:1").Delete Shift:=xlUp wr.CheckCompatibility = False wr.Save wr.CheckCompatibility = True appexcel.Visible = False wr.Close Set wr = Nothing Set appexcel = Nothing ' Import Data into tblFromIQY DoCmd.TransferSpreadsheet TransferType:=acImport, SpreadsheetType:=acSpreadsheetTypeExcel8, tableName:="tblFromIQY", fileName:=Path & rbFileName, _ HasFieldNames:=True Debug.Print ">>> Imported data to tblFromIQY" End Function 

经过一些Googlesearch,我发现这个解决scheme:

 Set wrksheet = appexcel.worksheets("Main Worksheet") wrksheet.Range("A:BN").QueryTable.Refresh BackgroundQuery:=False 

有关BackgroundQuery的文档。