none
Word to PowerPoint with C# RRS feed

  • Question

  • Hello,

    I'm writing a C# code, the following one.

    //save the html content to a file
                                                    File.WriteAllText(temporaryFilePath, html);
                                                    //insert the html file next to the shape
                                                    //shape.Range.InsertFile(temporaryFilePath); -> for Word, works
                                                    slide.Shapes.Range.InsertFile(temporaryFilePath);   -> Error
                                                    //remove the shape preview
                                                    shape.Delete();
                                                    fileToDelete.Add(temporaryFilePath);

    This works fine for Word, but I want to do it for PowerPoint.

    Since PowerPoint doesn't allow html files, I thought about implementing it in a textbox, but I have no clue about how to implement this with C#.

    Of course my error comes from the fact that it doesn' exist ".Range.InsertFile" with shapes, but I want to embed the html file next to the image.

    And I don't really find any news on www how to implement it in an other way.

    Some open ideas maybe?

    Thanks.



    • Moved by Bob Shen Friday, March 15, 2013 11:27 AM
    Wednesday, March 13, 2013 10:34 AM

Answers

  • Mike, you may look at the following code:

    '' Insert a html file
    ActivePresentation.Slides(3).Shapes.AddOLEObject     Width:=200, Height:=300, _
        FileName:="C:\Users\v-yoyj\Desktop\test.html", DisplayAsIcon:=msoCTrue
    '' Insert text
    ActivePresentation.Slides(3).Shapes.AddShape(msoShapeRectangle, 180, 175, 350, 140).TextFrame.TextRange.Text = "Here is some test text"

    We can also insert a word document with the following code:

    ActivePresentation.Slides(3).Shapes.AddOLEObject nbsp;   Width:=500, Height:=300, _
        FileName:="D:\00-Word\Test.docx", Link:=msoCTrue

    Refer to: 

    Shapes.AddOLEObject Method

    Shapes.AddShape Method

    Good day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 15, 2013 12:46 PM
    Moderator

  • Hello!

    Sorry for the delay and thanks a lot for the replies!!

    I found another way to do it.

    shape.Select();
    Console.WriteLine(shape.AlternativeText);
     //Console.Read();
    /remove the shape preview and read the html file stream and save as image
    shape.Delete();
    //we insert the real picture at the old shape position
    // http://msdn.microsoft.com/en-us/library/office/ff745953.aspx
    Console.WriteLine("temporary file path" + temporaryFilePath);
    WebsiteToImage websiteToImage = new WebsiteToImage(temporaryFilePath, @"C:\Users\vh610\AppData\Local\Temp\image.bmp");
    websiteToImage.Generate();
    slide.Shapes.AddPicture(@"C:\Users\vh610\AppData\Local\Temp\image.bmp", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, oldshapeleft, oldshapetop, oldshapewidth, oldshapeheight);                                         

    And WebsiteToImage was used to make actually a screenshot of the html file and paste it as a picture ^^

    stackoverflow.com/questions/2715385/convert-webpage-to-image-from-asp-net

    I was trying to implement it only in one way instead to try it in another way.

    Continuing now the next issue :)

    But the topic was answered.

    Thanks.

    • Marked as answer by mike27015 Wednesday, March 20, 2013 12:32 PM
    Wednesday, March 20, 2013 12:31 PM

All replies

  • Hello,

    I'm writing a C# code, the following one.

    //save the html content to a file
                                                    File.WriteAllText(temporaryFilePath, html);
                                                    //insert the html file next to the shape
                                                    //shape.Range.InsertFile(temporaryFilePath); -> for Word, works
                                                    slide.Shapes.Range.InsertFile(temporaryFilePath);   -> Error
                                                    //remove the shape preview
                                                    shape.Delete();
                                                    fileToDelete.Add(temporaryFilePath);

    This works fine for Word, but I want to do it for PowerPoint.

    Since PowerPoint doesn't allow html files, I thought about implementing it in a textbox, but I have no clue about how to implement this with C#.

    Of course my error comes from the fact that it doesn' exist ".Range.InsertFile" with shapes, but I want to embed the html file next to the image.

    And I don't really find any news on www how to implement it in an other way.

    Some open ideas maybe?

    Thanks.
    Wednesday, March 13, 2013 10:43 AM
  • Hi,

    Try this link it might be help 

    http://www.uhlme.ch/convert_office_to_xps

    Thanks

    Wednesday, March 13, 2013 11:40 AM
  • just a curious question from the side, why html? There are numerous examples of moving xml content in and out of both powerpoint and word.

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

    Wednesday, March 13, 2013 11:53 AM
  • Hi,

    I don't want to convert my powerpoint.

    I want to import a html file that was created in C# into a powerpoint slide.

    The goal is, to have a powerpoint template which has shapes and once I run my program, it will replace all shapes with the input from another program.

    And I have to implement the HTML code next to the shape.

    For word it works as mentioned above, but it doesn't for powerpoint
    Wednesday, March 13, 2013 12:01 PM
  • Because my company did it with HTML in Word.

    The main idea is to create a powerpoint template , for example, every month you get a graphical report of the amount of people who went to a supermarket, and you want to create every month a new report, but the template is the same, only picture ( which is a table ) from report are getting changed. The way my company did that is, while attaching the html file in the new powerpoint instead of the template picture( which is a table ) it is replaced by html code to create a real table with values and so on.

    Those pictures are always objects taken from a 3rd software that does Business Intelligence graphs.

    If I do it in html, and the table is like 5 powerpoint pages, I can subdivide the table and show in every new slide a piece of the table.

    Get the idea what I mean?

    Would like to share more code, but can't. ( Company privacy )

    Small code block:

    foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in presentation.Slides)
    {
          foreach (Microsoft.Office.Interop.PowerPoint.Shape shape in slide.Shapes)
          {
                                                string html = Clipboard.GetText(TextDataFormat.Html);
                                                int start = html.IndexOf("<HEAD>");
                                                int end = html.IndexOf("</TABLE>");
                                                if (start != -1)
                                                {
                                                    html = html.Substring(start, ((end + 9) - start - 1));
                                                    //Instantiating Regex object
                                                    Regex reTH = new Regex(@"<TH", RegexOptions.IgnoreCase);
                                                    Regex reTD = new Regex(@"<TD", RegexOptions.IgnoreCase);
                                                    Match m = reTH.Match(html);
                                                    Match mTD = reTD.Match(html);
                                                    if (reTH.IsMatch(html))
                                                    {
                                                        html = reTH.Replace(html, "<TH class='convertTableHeader' ");
                                                    }
                                                    if (reTD.IsMatch(html))
                                                    {
                                                        html = reTD.Replace(html, "<TD class='convertTableCell' ");
                                                    }
    
                                                    html = html.Replace("NOWRAP", "");
                                                    html = html.Replace("</HEAD>", "<style type='text/css'>.convertTable {border-width:1px;border-style:solid;border-color:#8DAACB;border-collapse:collapse;margin:0px; padding:0px; width:100%;}.convertTableCell {font-size:9px;font-family:Calibri;white-space: nowrap;margin:0px; padding:0px;}.convertTableHeader {font-size:12px;font-family:Calibri;white-space: nowrap;font-weight:bold;background-color:#ECECEC;}</style></HEAD>");
                                                    html = html.Replace("<TABLE ", "<TABLE class='convertTable'");
                                                    html = html.Replace("CELLSPACING=0 ", "CELLSPACING=0 ");
                                                    html = html.Replace("CELLPADDING=2 ", "CELLPADDING=0 ");
                                                    html = html.Trim();
                                                    html = html.Replace("/n/p/v", "");
                                                    //Console.WriteLine("------html: " + html + " --------");
                                                    //Console.Read();
    
                                                    //save the html content to a file
                                                    File.WriteAllText(temporaryFilePath, html);
                                                    //insert the html file next to the shape
                                                    //shape.Range.InsertFile(temporaryFilePath);
                                                    //slide.Shapes.AddPicture(html);
                                                    //slide.Shapes.Paste(temporaryFilePath);
                                                    //remove the shape preview
                                                    shape.Delete();
                                                    fileToDelete.Add(temporaryFilePath);
                                                }
                              

    And for Word it was almost the same just the 2 first loops are changed.

                    foreach (NetOffice.WordApi.InlineShape s in docWord.InlineShapes)
                    {
                        #region Set Shapes
                        if (s.Type==NetOffice.WordApi.Enums.WdInlineShapeType.wdInlineShapePicture &&  s.AlternativeText.Contains("|"))
                        {
                                            File.WriteAllText(temporaryFilePath, html);
                                            //insert the html file next to the shape
                                            s.Range.InsertFile(temporaryFilePath);
                                            //remove the shape preview
                                            s.Delete();
                                            fileToDelete.Add(temporaryFilePath);


    And

    shape.Range.InsertFile(temporaryFilePath);

    Doesn't work in PowerPoint.

    I tried

    slide.Shapes.Paste(temporaryFilePath);

    But same issue.

    All done in C#

    Thanks for help anyway.


    • Edited by mike27015 Wednesday, March 13, 2013 12:59 PM
    Wednesday, March 13, 2013 12:51 PM
  • thanks, maybe one of the word/powerpoint specialists can help based on the additional information.

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

    Wednesday, March 13, 2013 1:53 PM
  • Mike, you may look at the following code:

    '' Insert a html file
    ActivePresentation.Slides(3).Shapes.AddOLEObject     Width:=200, Height:=300, _
        FileName:="C:\Users\v-yoyj\Desktop\test.html", DisplayAsIcon:=msoCTrue
    '' Insert text
    ActivePresentation.Slides(3).Shapes.AddShape(msoShapeRectangle, 180, 175, 350, 140).TextFrame.TextRange.Text = "Here is some test text"

    We can also insert a word document with the following code:

    ActivePresentation.Slides(3).Shapes.AddOLEObject nbsp;   Width:=500, Height:=300, _
        FileName:="D:\00-Word\Test.docx", Link:=msoCTrue

    Refer to: 

    Shapes.AddOLEObject Method

    Shapes.AddShape Method

    Good day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 15, 2013 12:46 PM
    Moderator
  • Mike, have you got any update for this problem?

    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 19, 2013 6:02 AM
    Moderator

  • Hello!

    Sorry for the delay and thanks a lot for the replies!!

    I found another way to do it.

    shape.Select();
    Console.WriteLine(shape.AlternativeText);
     //Console.Read();
    /remove the shape preview and read the html file stream and save as image
    shape.Delete();
    //we insert the real picture at the old shape position
    // http://msdn.microsoft.com/en-us/library/office/ff745953.aspx
    Console.WriteLine("temporary file path" + temporaryFilePath);
    WebsiteToImage websiteToImage = new WebsiteToImage(temporaryFilePath, @"C:\Users\vh610\AppData\Local\Temp\image.bmp");
    websiteToImage.Generate();
    slide.Shapes.AddPicture(@"C:\Users\vh610\AppData\Local\Temp\image.bmp", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, oldshapeleft, oldshapetop, oldshapewidth, oldshapeheight);                                         

    And WebsiteToImage was used to make actually a screenshot of the html file and paste it as a picture ^^

    stackoverflow.com/questions/2715385/convert-webpage-to-image-from-asp-net

    I was trying to implement it only in one way instead to try it in another way.

    Continuing now the next issue :)

    But the topic was answered.

    Thanks.

    • Marked as answer by mike27015 Wednesday, March 20, 2013 12:32 PM
    Wednesday, March 20, 2013 12:31 PM