none
Génération powerpoint avec Graphique en C# RRS feed

  • Question

  • Bonjour,

    Je souhaite créer un document Powerpoint à partir d'un programme C#.
    Mais je rencontre le problème suivant lorsque je créé un graphique et que j'essai de le récuperer pour le modifier.
    Je ne comprend pas, si quelqu'un à une idée. Merci d'avance

    //Create a new PowerPoint application

    PowerPoint.

    Application ppApp = new PowerPoint.Application();

     

    // Create a new PowerPoint presentation.

    PowerPoint.

    _Presentation objPres = ppApp.Presentations.Add(MsoTriState.msoTrue);

     

    // Add a slide to the presentation.

    PowerPoint.

    _Slide objSlide = objPres.Slides.Add (1, PowerPoint.PpSlideLayout.ppLayoutBlank);

     

    //Add a chart to the slide

    PowerPoint.

    Shape objShape = objSlide.Shapes.AddOLEObject(0, 0, 150, 150, "MSGraph.Chart", "", 0, "", 0, "", 0);

    Graph.

    Chart objChart;

    objChart = (Graph.

    Chart)objShape.OLEFormat.Object;

     

     

    // Save the presentation

    objPres.SaveAs(

    "C:\\ChartTest.ppt", PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, MsoTriState.msoTrue);

     



    Impossible d'effectuer un cast d'un objet COM de type 'System.__ComObject' en type d'interface 'Graph.Chart'. Cette opération a échoué, car l'appel QueryInterface sur le composant COM pour l'interface avec l'IID '{000208FB-0000-0000-C000-000000000046}' a échoué en raison de l'erreur suivante : Cette interface n'est pas prise en charge (Exception de HRESULT : 0x80004002 (E_NOINTERFACE)).



    Impossible d'effectuer un cast d'un objet COM de type 'System.__ComObject' en type d'interface 'Graph.Chart'. Cette opération a échoué, car l'appel QueryInterface sur le composant COM pour l'interface avec l'IID '{000208FB-0000-0000-C000-000000000046}' a échoué en raison de l'erreur suivante : Cette interface n'est pas prise en charge (Exception de HRESULT : 0x80004002 (E_NOINTERFACE)).

    mardi 8 septembre 2009 11:59

Réponses

  • Salut Rastanet,
    Ce code fonctionne sur mon ordinateur.

    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 PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Graph = Microsoft.Office.Interop.Graph;
    using Core = Microsoft.Office.Core;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                //Create a new PowerPoint application
    
                PowerPoint.Application ppApp = new PowerPoint.Application();
    
                // Create a new PowerPoint presentation.
    
                PowerPoint.Presentation objPres = ppApp.Presentations.Add(Core.MsoTriState.msoTrue);
    
                // Add a slide to the presentation.
    
                PowerPoint.Slide objSlide = objPres.Slides.Add(1,PowerPoint.PpSlideLayout.ppLayoutBlank);
    
                //Add a chart to the slide
    
                PowerPoint.Shape objShape = objSlide.Shapes.AddOLEObject(0, 0, 150, 150, "MSGraph.Chart", "", 0, "", 0, "", 0);
    
                Graph.Chart objChart;
    
                objChart = (Microsoft.Office.Interop.Graph.Chart)objShape.OLEFormat.Object;
    
                // Save the presentation
    
                objPres.SaveAs("C:\\ChartTest.ppt",PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,Core.MsoTriState.msoTrue);
                objPres.Close();
                
            }
        }
    }
    Ce pourrait être un problème avec les références?
    Tu as ajouté toutes les références en utilisant l'onglet .NET?
    Cordialement,
    Marius
    jeudi 10 septembre 2009 01:58

Toutes les réponses

  • Salut Rastanet,
    Ce code fonctionne sur mon ordinateur.

    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 PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Graph = Microsoft.Office.Interop.Graph;
    using Core = Microsoft.Office.Core;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                //Create a new PowerPoint application
    
                PowerPoint.Application ppApp = new PowerPoint.Application();
    
                // Create a new PowerPoint presentation.
    
                PowerPoint.Presentation objPres = ppApp.Presentations.Add(Core.MsoTriState.msoTrue);
    
                // Add a slide to the presentation.
    
                PowerPoint.Slide objSlide = objPres.Slides.Add(1,PowerPoint.PpSlideLayout.ppLayoutBlank);
    
                //Add a chart to the slide
    
                PowerPoint.Shape objShape = objSlide.Shapes.AddOLEObject(0, 0, 150, 150, "MSGraph.Chart", "", 0, "", 0, "", 0);
    
                Graph.Chart objChart;
    
                objChart = (Microsoft.Office.Interop.Graph.Chart)objShape.OLEFormat.Object;
    
                // Save the presentation
    
                objPres.SaveAs("C:\\ChartTest.ppt",PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,Core.MsoTriState.msoTrue);
                objPres.Close();
                
            }
        }
    }
    Ce pourrait être un problème avec les références?
    Tu as ajouté toutes les références en utilisant l'onglet .NET?
    Cordialement,
    Marius
    jeudi 10 septembre 2009 01:58
  • Salut Rastanet,
    Est-ce que tu as résolu le problème?
    Cordialement,
    Marius
    vendredi 11 septembre 2009 15:14
  • Salut Marius,

    Je n'avais pas vu ta réponse, je n'ai pas encore essayé ton code.
    Je le ferais Lundi.

    Pour ce qui est des références, je n'ai pas utilisés les .NET mais les COM pour PowerPoint et Graph.

    Peut-être que sa viens de là. je test et je te tien au courant.

    En tout cas merci pour ton aide.
    samedi 12 septembre 2009 08:22
  • Salut marius,

    Ton code fonctionne bien, mais je ne vois pas vraiment de différence avec le mien.
    Peux être que j'ai un peu trop joué avec les références.

    Merci de ton aide

    lundi 14 septembre 2009 07:36
  • Je t'en prie.
    Moi aussi je ne sais pas pourquoi ça marche, mais important est que ça marche :)
    Je suppose que c’est un problème de COM ou de références.
    Cordialement,
    Marius
    • Proposé comme réponse Farah85 mardi 31 août 2010 07:35
    lundi 21 septembre 2009 15:03