title: VB.NET - Programatically close the chart data object
body: <p>My program is automating PowerPoint to loop through a series of chart parameters and create a new chart per parameter set. So far it works well for the first chart - however, it throws an error when attempting to create a second chart because the chart data grid is already open, and I can't find a method to properly close or dispose of the data grid after generating the graph.</p>
  
  <p>Abridged code:</p>
  
  <pre><code>Imports Powerpoint = Microsoft.Office.Interop.PowerPoint
  Imports Excel = Microsoft.Office.Interop.Excel
  
  Private Sub generatePowerPoint(Qnum As String)
  Try
     'Create PowerPoint object and assign a presentation / slide to it
          Dim oApp As Powerpoint.Application
          Dim oPres As Powerpoint.Presentation
          Dim oSlide As Powerpoint.Slide
  
          oApp = New Powerpoint.Application()
          oApp.Visible = True
          oApp.WindowState = Powerpoint.PpWindowState.ppWindowMinimized
  
          oPres = oApp.Presentations.Add
  
          'Prepare to generate charts based on parameters in a listbox
          Dim slideCount = lbQuestions.Items.Count
  
  
          For slideN = 1 To slideCount
           'Add a blank slide per graph request   
           oSlide = oPres.Slides.Add(slideN, Powerpoint.PpSlideLayout.ppLayoutBlank)
  
              'Create a new shape object for each slide
              Dim chartShape(slideCount) As Powerpoint.Shape
  
              ' What's causing the error: assign a chart object to the next shape object.
              ' This works for the first slide, but then throws an error that the PowerPoint 
              'Chart Data Grid is still open, preventing it from creating a new chart.
  
              chartShape(slideN - 1) = oSlide.Shapes.AddChart2(-1, ChartFind(chartType), 50, 50, 775, 410)
              Dim cData = chartShape(slideN - 1).Chart.ChartData 'Activate to refresh
  
              Dim workbook = cData.Workbook
              workbook.Application.Visible = False
  
              Dim datasheet = workbook.Worksheets(1)
  
              Dim colNumber As Integer = 2
              Dim firstRowNumber As Integer = 2
  
              datasheet.rows.clear()
              datasheet.columns.clear()
  
              For r = categoryNames.Count - 1 To 0 Step -1 
  
                  datasheet.Cells(r + firstRowNumber, 1) = categoryNames(r)
  
              Next
  
        ... Code to assign data and format the chart object ...
  
             'Refresh the range accepted by the chart object
              chartShape(slideN-1).Chart.Refresh
       'Loop again
         Next
  </code></pre>
  
  <p>I've spent some time going through the PowerPoint Interop docs and the PowerPoint Chart Object Model docs on msdn (e.g. <a href="https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/ff760412(v=office.14)" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/ff760412(v=office.14)</a>, <a href="https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/chartdata-object-powerpoint" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/chartdata-object-powerpoint</a>), and it seems that there's while there's a method to call the Chart Data Grid (chartdata.activate()) , there isn't a method to close the Chart Data Grid.</p>
  
  <p>The exact error message thrown is "System.Runtime.InteropServices.COMException (0xBFFF64AA): The chart data grid is already open in 'Presentation 1 - PowerPoint'. To edit the data for this chart, you need to close it first. at Microsoft.Office.Interop.PowerPoint.Shapes.AddChart2( ..."</p>
  
  <p>Does anyone have a suggestion?</p>