title: Unable to set the Visible property of the PivotItem class (VBA)
body: <p>I am trying to manipulate an Excel 2007 Pivot Table trough VBA so I can loop trough the categories of the pivot table, set all to invisible but one, save the sheet as pdf and continue to the next category. For this I use the following piece of code.</p>
  
  <pre><code>Dim pf As PivotField
  Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("NAME")
  
  Dim pi as PivotItem
  For Each pi In pf.PivotItems
  
      If pi.Visible = False Then
          pi.Visible = True 'Error here
      End If
  
      Dim pi2 As PivotItem
      For Each pi2 In pf.PivotItems
          If pi2 &lt;&gt; pi Then
              pi2.Visible = False
          End If
      Next pi2
  
      'Saving to PDF goes here
  Next pi
  </code></pre>
  
  <p>The loop seems to be working the first time. Every category gets deselected but the first and it outputs a nice PDF file. The next time it enters the loop however it gives an 'Unable to set the Visible property of the PivotItem class' error at the indicated line. I am aware of the fact that in a pivot table there has to be at least one item selected but that is not the problem here as I am trying to set the visibility to TRUE instead of FALSE.</p>
  
  <p>I tried fixing it by putting a check around it as maybe you are not allowed to set an already visible PivotItem to visible but that did not seem to work.</p>
  
  <p>Any help would be very much appreciated!</p>