IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Windows Discussion :

[C#] Modifier un Chart dans Powerpoint


Sujet :

Développement Windows

  1. #1
    Candidat au Club
    Inscrit en
    Juillet 2010
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 4
    Points : 4
    Points
    4
    Par défaut [C#] Modifier un Chart dans Powerpoint
    Bonjour tout le monde,

    j'ai pour projet de faire une application qui crée automatiquement un powerpoint.
    Mais j'ai un soucis au niveau de la modification d'un chart qui est dans un template (fichier .pot). Voici un snipped de mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    // ceci est ma methode pour modifier un slide
    public void ModifiedSlide3(PowerPoint.Slide objSlide, PowerPoint.Presentations objPresSet, PowerPoint._Presentation objPres, int i, String title)
            {
                // instanciation de ma classe Form1
                Form1 f = new Form1();
     
                // methode qui contient de methode pour acceder a ma base de donnee
                Dao d = new Dao();
     
                // Integer i permet de choisir le slide.
                objSlide = objPres.Slides[i];
                int e = f.CountTable(objSlide, i);
     
                e = e + 1;
     
    objSlide.Shapes.AddTextbox(Core.MsoTextOrientation.msoTextOrientationHorizontal, 3, 90, 125, 95);
                objTextRng = objSlide.Shapes[e].TextFrame.TextRange;
                objTextRng.Text = title;
                objTextRng.Font.Name = "Trebucher MS";
                objTextRng.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;
                System.Diagnostics.Debug.Write("Counter = "+ e +" \n");
                int p = objSlide.Shapes.Count + 1;
                System.Diagnostics.Debug.Write("La valeur de p : " + p + "\n");
     
                // Selection d'un chart
                objChart = (Graph.Chart)objSlide.Shapes[1].OLEFormat.Object;
                datasheet = objChart.Application.DataSheet;
                datasheet.Cells[7, 7] = "60";
                objChart.Application.Update();
     
            }
    Au niveau de la compilation, je recois cette erreur :
    Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Graph.Chart'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208FB-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
    Mais par contre j'arrive a ajouter un chart grace a ce code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    objChart = (Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320,
                "MSGraph.Chart.8", "", Core.MsoTriState.msoFalse, "", 0, "",
                Core.MsoTriState.msoFalse).OLEFormat.Object;
    Avez vous une idée au niveau de l'erreur générée ou un autre moyen de pouvoir modifier un chart avec une autre méthode?

  2. #2
    Candidat au Club
    Inscrit en
    Juillet 2010
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 4
    Points : 4
    Points
    4
    Par défaut Alternative à la modification d'un Chart Graphique sous Powerpoint
    Bonjour,

    Finalement j'ai opté pour une création d'un chart sous excel et l'importation de ce dernier dans mon powerpoint!!! Voici le code pour ce qui ca interesserai!!! Après plusieurs recherches sur des forums anglophones, je me suis apercu qu'on ne pouvait pas modifier un chart deja créer et intégrer dans powerpoint (Peut-être avec le Version 2010 de Powerpoint)

    Pour précision, il faut importer les Références Powerpoint et Excel :
    Microsoft.Office.Interop.Excel;
    Microsoft.Office.Interop.Powerpoint;

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
     
     
    using xlNS = Microsoft.Office.Interop.Excel;
    using pptNS = Microsoft.Office.Interop.PowerPoint;
     
    ....
     
    public class ExcelToPowerpoint {
     
            pptNS.ApplicationClass powerpointApplication = null;
            pptNS.Presentation pptPresentation = null;
            pptNS.Slide pptSlide = null;
            pptNS.ShapeRange shapeRange = null;
     
            xlNS.ApplicationClass excelApplication = null;
            xlNS.Workbook excelWorkBook = null;
            xlNS.Worksheet targetSheet = null;
            xlNS.ChartObjects chartObjects = null;
            xlNS.ChartObject existingChartObject = null;
            xlNS.Range targetRange = null;
     
            object misValue = System.Reflection.Missing.Value;
            string paramPresentationPath = "C:\\Path\\to\\your\\ppt";
            string paramWorkbookPath = "C:\\Path\\to\\your\\xls";
            object paramMissing = Type.Missing;
     
    powerpointApplication = new pptNS.ApplicationClass();
     
                    // Create an instance Excel.          
                    excelApplication = new xlNS.ApplicationClass();
     
                    // Open the Excel workbook containing the worksheet with the chart
                    // data.
                    excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing);
                    // Get the worksheet that contains the chart.
                    targetSheet =
                        (xlNS.Worksheet)(excelWorkBook.Worksheets["Graphique1"]);
                    // Modify a data on a cell
                    Form1 f = new Form1();
                    f.AddData(targetSheet, targetRange, 5, 2, "0.80", "B5", "B5", "0.0%");
                    // Get the ChartObjects collection for the sheet.
                    chartObjects =
                        (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));
                    // Get the chart to copy.
                    existingChartObject =
                        (xlNS.ChartObject)(chartObjects.Item("Chart 4"));
                    // Create a PowerPoint presentation.
                    pptPresentation = powerpointApplication.Presentations.Add(
                                        Microsoft.Office.Core.MsoTriState.msoTrue);
                    // Add a blank slide to the presentation.
                    pptSlide =
                        pptPresentation.Slides.Add(1, pptNS.PpSlideLayout.ppLayoutBlank);
                    // Copy the chart from the Excel worksheet to the clipboard.
                    existingChartObject.Copy();
                    // Paste the chart into the PowerPoint presentation.
                    shapeRange = pptSlide.Shapes.Paste();
                    // Position the chart on the slide.
                    shapeRange.Left = 60;
                    shapeRange.Top = 100;
     
                    // Save the excel
                    //excelWorkBook.SaveAs(paramWorkbookPath, xlNS.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, xlNS.XlSaveAsAccessMode.xlExclusive, xlNS.XlSaveConflictResolution.xlOtherSessionChanges, misValue, misValue, misValue, misValue);
                    excelWorkBook.Save();
                    excelWorkBook.Close(false, paramMissing, paramMissing);
                    System.Diagnostics.Debug.Write("je suis passe par la18\n");
                    // Save the presentation.
                    pptPresentation.SaveAs(paramPresentationPath,
                                    pptNS.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
                                    Microsoft.Office.Core.MsoTriState.msoTrue);
                    pptPresentation.Close();
     
     
                    // Method for modify a data on my excel File
                    public void AddData(xlNS.Worksheet targetSheet, xlNS.Range targetRange, int row, int col, string data, string cell1, string cell2, string format)
            {
                System.Diagnostics.Debug.Write("Get Buck in Here\n");
                targetSheet.Cells[row, col] = data;
                System.Diagnostics.Debug.Write("Get Buck in Here2\n");
                targetRange = targetSheet.get_Range(cell1, cell2);
                System.Diagnostics.Debug.Write("Get Buck in Here3\n");
                targetRange.NumberFormat = format;
                System.Diagnostics.Debug.Write("Get Buck in Here4\n");
            }
    }
    Par contre, j'aimerais savoir comment insérer le fichier excel dans le chart???

    Merci d'avance pour vos réponses.

  3. #3
    Membre chevronné
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    1 048
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2009
    Messages : 1 048
    Points : 2 201
    Points
    2 201
    Par défaut
    Hello

    L'automation de Office par objet COM n'est pas la méthode la plus propre. (Entre autre parce que parfois ça plante sans vraiement savoir pourquoi)

    Si tu travaille avec Office 2007 ou plus, il est recommandé de travailler directement le fichier en XML. L'utilisation de VSTO peut aussi être une idée en fonction de l'objectif final de ton application.

Discussions similaires

  1. Modifier une zone de texte avec VBA dans powerpoint
    Par Akerman dans le forum VBA PowerPoint
    Réponses: 1
    Dernier message: 21/12/2009, 07h05
  2. Comment faire pour modifier une ligne dans une DBGrid?
    Par Nico62 dans le forum C++Builder
    Réponses: 6
    Dernier message: 29/03/2005, 13h24
  3. Modifier l'utilisateur dans une PS ?
    Par Sitting Bull dans le forum SQL
    Réponses: 7
    Dernier message: 28/02/2005, 19h01
  4. modifier les texte dans un <span>
    Par Flobel dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 03/12/2004, 14h58
  5. Modifier un événement dans le code
    Par HT dans le forum Langage
    Réponses: 6
    Dernier message: 20/06/2003, 10h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo