Update and save excel file using C #

I use this code to open a save and close excel file:

Application excelFile = new Application();               
Workbook theWorkbook = excelFile.Workbooks._Open(Environment.CurrentDirectory + "/WebGate", 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false);    
Sheets sheets = (Sheets)theWorkbook.Worksheets;    
theWorkbook.RefreshAll();    
theWorkbook.Save();    
excelFile.Quit();

the problem is that there is a conflict between update and save commands because the file is saved and the update is running in the background (I think) Can anyone help me with this? I need to know how I can find out when the update process is running or some other indication that will help me decide when I should save the file without affecting the update process

+3
source share
4 answers

#, Excel VBA. , Pivot BackgroundQuery, True, , Excel . - , , BackgroundQuery - > - > BackgroundQuery " ". , false,

Dim oPivot As PivotTable
set oPivot=worksheets("xyz").PivotTables("Pivot1") 
oPivot.PivotCache.BackgroundQuery = False

, excel, Excel VBA, .

Public Sub FixPivotTables()
    Dim oPivot As New PivotTable, oSheet As Worksheet
    For Each oSheet In ThisWorkbook.Worksheets
        For Each oPivot In oSheet.PivotTables
            oPivot.PivotCache.BackgroundQuery = False
        Next
    Next
End Sub
+4

, , .

QueryTable, . "AfterRefresh", - .

, , Refresh on workbook, Refresh QueryTable ( QueryTables). QueryTable Refresh, BackGroundQuery, False.

, .
?

+3

, , .

, Pivottables, - .
PivotTableCloseConnection , Save .

Excel 2007 Application AfterCalculate, .

?

+2

Try using:

this.Application.ActiveWorkbook.RefreshAll();
this.Application.ActiveWorkbook.Save();

Use an action if necessary .ReCalculate().

0
source

All Articles