0
votes

I have a VSTO addin that receives an excel chart (in byte array) over a network from a server. I would like to paste this chart onto a powerpoint slide programmatically without having to save the chart to disk first.

However, when I look through all the .Add* methods exposed by Microsoft.Office.Interop.PowerPoint.Shapes, they all seem to require a file path in string . In other words, I would have to convert the binary data to a file in a supported format on the system and get its path before I can use those functions.

Are there any way to directly use this binary data and paste it on the powerpoint slide without having to save it as a file on the system first?

1
your question is about pasting chart, why binary data matters? if the data is from some text format, will you easily accomplish your task?Lei Yang
@LeiYang binary data matters because that is the format of the chart that my app received over the network via TCP protocol from another application. Even if it is in a text format, I would still have trouble pasting it directly onto the slide because all the methods to embed onto powerpoint slide exposed by Microsoft.Office.Interop.PowerPoint.Shapes only accept the path of the file in string-not the file itself-as its parameter to access the required file... if I'm not mistaken.John Evans Solachuk
so actually you don't need emphasize the data source type in this question. your biggest problem lies somewhere else.Lei Yang
@LeiYang does this mean that everytime I want to embed a file onto a content, I would have to feed the methods with a file path? I was wondering if there is a method which allows me to feed it with byte arrays of the file so I don't have to waste time writing file to disk first in order to embed it to slide.John Evans Solachuk
there should not be such method.Lei Yang

1 Answers

-1
votes
using Microsoft.Office.Interop.Excel;  
  
void CreateChart()
{
    ChartData gChartData;
    Workbook gWorkBook;
    Worksheet gWorkSheet;
    //  Create the chart and set a reference to the chart data.
    var myChart = ActivePresentation.Slides[1].Shapes.AddChart() as Microsoft.Office.Interop.PowerPoint.Chart;
    gChartData = myChart.ChartData;
    //  Set the Workbook and Worksheet references.
    gWorkBook = gChartData.Workbook;
    gWorkSheet = gWorkBook.Worksheets[1];
    //  Add the data to the workbook.
    gWorkSheet.ListObjects["Table1"].Resize(gWorkSheet.Range["A1:B5"]);

    gWorkSheet.Range["Table1[[#Headers],[Series 1]]"].Value = "Items";

    gWorkSheet.Range["a2"].Value = "Coffee";
    gWorkSheet.Range["a3"].Value = "Soda";
    gWorkSheet.Range["a4"].Value = "Tea";
    gWorkSheet.Range["a5"].Value = "Water";
    gWorkSheet.Range["b2"].Value = "1000";
    gWorkSheet.Range["b3"].Value = "2500";
    gWorkSheet.Range["b4"].Value = "4000";
    gWorkSheet.Range["b5"].Value = "3000";

    //ToDo: Style
}

fill the data as you like (you may fill it with a for loop and fill the $"a{i}" column with the number of the entry (i))

See this Article for further Information

UPDATE 21.04.2021:

This isn't what the OP asked for.

In his comment he said:

Even if it is in a text format, I would still have trouble pasting it directly onto the slide because all the methods to embed onto PowerPoint slide exposed by Microsoft.Office.Interop.PowerPoint.Shapes only accept the path of the file in string-not the file itself-as its parameter to access the required file... if I'm not mistaken.

This is not correct. I pointed out a way to create charts programmatically without having to create an excel sheet an save it somewhere.

If you would have gotten the data in a defined format (e.g. json) you could use my code to generate the chart.

But now what you seem to be waiting for:

If you click copy with and excel-chart selected, then open your PowerPoint presentation, set your selection where your want the chart to be and paste the chart, it will be inserted there no problem.

But how can I sent this Data and then paste it?

  1. The Excel-Chart has got a method to copy itself: https://docs.microsoft.com/de-de/office/vba/api/excel.chart(object)
  2. Convert the data in your clipboard to binary data
  3. Send the data
  4. Convert the data back and store it in your clipboard
  5. Use the TextRange of your Powerpoint-Selection to paste the data https://docs.microsoft.com/de-de/office/vba/api/powerpoint.textrange