title: Listing and refreshing all Pivot tables
body: <p>I'm trying to create a script that refresh all data in a worksheet and then refreshes the pivot tables afterward (because data in pivot tables are normally refreshed before the data from Databases the result is not correct by default).</p>
  
  <p>To do this i use this script (because I'm starting this script automatically each day at 9.00).</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Runtime.InteropServices;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
  
              // Get fully qualified path for xlsx file
              var spreadsheetLocation = "C:\\update_rapport\\Salgsrapport.xlsx";
  
              var exApp = new Microsoft.Office.Interop.Excel.Application();
              var exWbk = exApp.Workbooks.Open(spreadsheetLocation);
              //var exWks = (Microsoft.Office.Interop.Excel.Worksheet)exWbk.Sheets["responses(7)"];
  
              exWbk.RefreshAll();
  
              exApp.DisplayAlerts = false;
  
              // This part is not correct. Need to find all pivot tables and update them
              Object PivotTables(
                  Object Index
              );
  
  
              string save_file_name = "C:\\temp\\updated\\Salgsrapport.xlsx";
              exWbk.SaveAs(save_file_name);
              exWbk.Close(true);
              exApp.Quit();
  
          }
  
      }
  }
  </code></pre>
  
  <p>The closest thing i have found to looping through all pivot tables is this:
  <a href="http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.pivottables.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.pivottables.aspx</a></p>
  
  <p>However it's not a complete example, and I have never really programmed C# before so I'm kind of lost here. There might be a simpler solution to this problem, any hint is appreciated.</p>