Reputation: 175
I have created VBA code that updates an Excel sheet, refreshes the Data Connection and saves the Excel sheet.
The problem : while the Data connection is still getting refreshed, the VBA code moves to the next step of Saving the Excel, and a pop-up prompting the user to Cancel the refresh appears (which it shouldn't because the refresh has to happen and a user input defeats the purpose of the VBA code). Below is a snippet:
wkb.Connections("Connection2").Refresh
While (InStr(1, Application.DisplayStatusBar, "Running background query") > 0)
Wend
wkb.Save
wkb.RefreshAll
While (InStr(1, Application.StatusBar, "Running background query") > 0)
Wend
wkb.Save
Question : How can I capture this text in the Status Bar so that I can wait till it disappears?
Application.StatusBar does NOT do the trick.
Upvotes: 3
Views: 1991
Reputation: 696
Here's a few things to try:
1) Go to the connection properties and uncheck "Enable background refresh". It should wait for the refresh to end before executing your next line of code. Or in code:
ActiveWorkbook.Connections("CONNECTION_NAME").ODBCConnection.BackgroundQuery = False
2) Try the method CalculateUntilAsyncQueriesDone
Connections("CONNECTION_NAME").Refresh
Application.CalculateUntilAsyncQueriesDone
If you're working in Excel 2010 or later it should wait until this is all done.
3) Possible third option, just a DoEvents command
Connections("CONNECTION_NAME").Refresh
DoEvents
Upvotes: 2