Extra Excel Application Opening while creating powerpoint charts

khalilmirza 96 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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,240 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,487 questions
PowerPoint Management
PowerPoint Management
PowerPoint: A family of Microsoft presentation graphics products that offer tools for creating presentations and adding graphic effects like multimedia objects and special effects with text.Management: The act or process of organizing, handling, directing or controlling something.
221 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,640 questions
0 comments No comments
{count} votes

Accepted answer
  1. khalilmirza 96 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,571 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.