I have the following code to open a worksheet, copy one chart, open a presentation and paste it.
It work fine, for one chart and one slide, but in the XLSM there's 8 charts and so 8 slides in PPTX, i don't know how to select for example, the second chart and paste it into the second or third slide of the presentation.
With the PowerPoint.Slide curSlide = pptApp.ActiveWindow.View.Slide; it selects the current slide or slide 1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using xlNS = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
xlNS.Application excelApplication = null;
xlNS.Workbook excelWorkBook = null;
xlNS.Worksheet targetSheet = null;
xlNS.ChartObjects chartObjects = null;
xlNS.ChartObject existingChartObject = null;
String Excelpath = "C:\\Users\\Diego\\Desktop\\Indicador Mensal.xlsm";
excelApplication = new xlNS.Application();//Instancia o excel e abre o XLSM
excelWorkBook = excelApplication.Workbooks.Open(Excelpath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
PowerPoint.Application pptApp = new PowerPoint.Application();
pptApp.Presentations.Open("C:\\Users\\Diego\\Desktop\\Teste.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue); //Abre o PPTX
PowerPoint.Slide curSlide = pptApp.ActiveWindow.View.Slide;
xlNS.Worksheet Ws = new xlNS.Worksheet();
Ws = (xlNS.Worksheet)excelWorkBook.Worksheets[1];//Número da Planilha que contém o gráfico
Ws.Activate();
targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets["Assumidos no Prazo"]);
chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(Type.Missing));
existingChartObject = (xlNS.ChartObject)(chartObjects.Item(1));
existingChartObject.Copy();
curSlide.Shapes.Paste();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
MessageBox.Show("Finalizado");
}
}
}
}
Solved the problem with this code: