question

khalilmirza avatar image
0 Votes"
khalilmirza asked khalilmirza edited

Extra Excel Application Opening while creating powerpoint charts

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.


dotnet-csharpoffice-excel-itprooffice-vba-devoffice-vsto-com-devoffice-powerpoint-itpro
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

khalilmirza avatar image
0 Votes"
khalilmirza answered khalilmirza edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

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.


capture.png (25.2 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are right those three lines are redundant.
I removed them but I am experiencing the same issue.

0 Votes 0 ·

Is there any forum where I could get this answered?
I am stuck on this for the last 20 days.

0 Votes 0 ·

@khalilmirza
Once in the MSDN forum, we had a forum dedicated to discussing VSTO-related issues, but now no one is responsible for related issues in Microsoft Q&A.
I have no experience in developing VSTO, so I can only try to simulate your problem with ordinary code as much as possible. I am sorry for not being able to provide an effective solution.

0 Votes 0 ·