I want to loop through a directory (using C#) and Refresh all Excel sheets in there. I use:
Workbook.RefreshAll();
How can I wait for the <
If you go through your Workbook
and set the the BackgroundQuery
property of all of your QueryTable
s to false
, RefreshAll
should finish before moving on.
foreach (Worksheet worksheet in workbook.Worksheets)
{
foreach (QueryTable table in worksheet.QueryTables)
table.BackgroundQuery = false;
}
//This will finish executing before moving on
workbook.RefreshAll();
Maybe you can query for the State:
Application.CalculationState =xldone
For Documentation look here: http://msdn.microsoft.com/en-us/library/office/bb220901(v=office.12).aspx
Updated 31.05.2016...
Thanks for your help. I found the following solution:
foreach (MSExcel.WorkbookConnection cnn in wb.Connections)
{
if (cnn.Type.ToString() == "xlConnectionTypeODBC")
{
cnn.ODBCConnection.BackgroundQuery = false;
}
else
{
cnn.OLEDBConnection.BackgroundQuery = false;
}
}
Frank
The below code will work perfectly when your excel has to run background query to pull data from SQL Server/MS Access and it will wait for the query is successful.
public void RefreshSheet(Excel.Application Excel, Excel.Workbook WB)
{
WB.RefreshAll();
Excel.Application.CalculateUntilAsyncQueriesDone();
WB.save();
}
Here WB is the workbook and Excel is the Excel Application declared in your code.