VB.NET-以编程方式关闭图表数据对象

时间:2018-08-02 17:54:02

标签: vb.net automation powerpoint

我的程序正在使PowerPoint自动循环一系列图表参数,并为每个参数集创建一个新图表。到目前为止,它对于第一个图表都工作良好-但是,由于第二个图表数据网格已经打开,因此在尝试创建第二个图表时会引发错误,并且在找到之后无法找到正确关闭或处理数据网格的方法生成图。

节略代码:

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

我花了一些时间浏览msdn上的PowerPoint Interop文档和PowerPoint Chart Object Model文档(例如https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/ff760412(v=office.14)https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/chartdata-object-powerpoint),而且似乎有一种方法可以调用图表数据网格(chartdata.activate()),没有关闭图表数据网格的方法。

引发的确切错误消息是“ System.Runtime.InteropServices.COMException(0xBFFF64AA):图表数据网格已在“演示文稿1-PowerPoint”中打开。要编辑此图表的数据,您需要先关闭它。at Microsoft.Office.Interop.PowerPoint.Shapes.AddChart2(...“

有人有建议吗?

1 个答案:

答案 0 :(得分:1)

大部分已解决。对于那些可能遇到相同问题的人:

chartShape.Chart.ChartData.Workbook.close()

这是一个未公开的方法/ IntelliSense将不会提供此方法(因此在大写时为Close),但是在打开图表对象并编辑数据之后,请确保在尝试创建新的图表对象之前完成此代码块

现在,如果打开的工作簿不是您打开的工作簿,则此方法不起作用(例如,我无法测试用户是否打开了工作簿,如果是,则将其关闭)。我通过将TryCatch方法封装为AddChart2方法来解决此问题,如果引发错误,我将通知用户关闭窗口并退出子例程,以使程序不会崩溃。