Extra Excel Application Opening while creating powerpoint charts

khalilmirza 101 Reputation points
2021-05-19T13:05:16.687+00:00

I am creating charts in PowerPoint. The below code opens two excel applications. One opens in the background that is invisible. The second one opens after the method ends. I need to make sure second excel either never open ideally or I can close it after it opens.

I have tried the below things but none worked.

I have tried forcing GC, Manual ReleaseComObject, Killing Excel process

I have tried separating excel COM objects and forcing GC

private void BtnInsert_Click(object sender, EventArgs e)
{
    var Addin = Globals.ThisAddIn;
    Microsoft.Office.Interop.PowerPoint.Application activeApplication = Addin.Application;
    DocumentWindow activeWindows = activeApplication.ActiveWindow;
    Microsoft.Office.Interop.PowerPoint.View activeView = activeWindows.View;
    Slide activeSlide = activeView.Slide;

    Microsoft.Office.Interop.PowerPoint.Shapes slideShape = activeSlide.Shapes;
    Microsoft.Office.Interop.PowerPoint.Shape shape = slideShape.AddChart2(-1, XlChartType.xl3DBarClustered, -1, -1, -1, -1, true);
    Microsoft.Office.Interop.PowerPoint.Chart chart = shape.Chart;

    //Access the chart data
    Microsoft.Office.Interop.PowerPoint.ChartData chartData = chart.ChartData;
    chartData.Activate();

    //Create instance to Excel workbook to work with chart data
    Workbook workbook = chartData.Workbook;

    //Accessing the data worksheet for chart
    Worksheet worksheet = workbook.Worksheets[1];

    // I am adding data here
    // This is not required to reproduce this

    chartData.BreakLink();
    workbook.Close(true);
}

Also, note that this issue does not occur while updating data.

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | PowerPoint | For business | Windows
Microsoft 365 and Office | Excel | For business | Windows
Developer technologies | Visual Basic for Applications
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. khalilmirza 101 Reputation points
    2021-05-24T02:33:02.9+00:00

    Remove
    chartData.Activate()
    and
    chartData.BreakLink()
    solves this.
    Although online documentation says that
    chartdata.activate
    is required before accessing the workbook.
    Otherwise, we will get a null reference.

    I think the documentation is incorrect or it does not apply to vsto.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-05-20T02:33:37.087+00:00

    I have no experience in developing VSTO, so I am using ordinary code to simulate your problem. If my description does not match your problem, please forgive me.

    In my test, the following line of code is enough to call up Chart in Microsoft Powerpoint:

    Workbook workbook = chartData.Workbook;  
    

    98056-capture.png
    The following code seems redundant:

                Microsoft.Office.Interop.Excel.Application workbookApplication = workbook.Application;  
                workbookApplication.Visible = false;  
                workbookApplication.WindowState = XlWindowState.xlMinimized;  
    

    It is these codes that open additional excel windows. Are they useful for other purposes? Can these codes be deleted?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.