none
How to add multiple slides to powerpoint simultaneously by threading RRS feed

  • Question

  • please see my code. notice that i am adding slides in foreach loop. if i have 7 country then 7 slides will be added to powerpoint file. so i am curious to know how could i add slides simultaneously instead of one after one.

    // Add a blank slide to the presentation.
    pptSlide = pptPresentation.Slides.Add(1, ppt.PpSlideLayout.ppLayoutBlank);
    
    foreach (var CountryCode in oNpsMain.CountryCodes)
    {
        // adding text as title in each slides
        oTxtShape = pptSlide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 
        oTxtShape.TextFrame.TextRange.Text = CountryCode + " " + oNpsMain.StartDate + " to " + oNpsMain.EndDate;
        oTxtShape.TextEffect.FontName = "Arial";
        oTxtShape.TextEffect.FontSize = 32;
        oTxtShape.TextEffect.FontBold = Microsoft.Office.Core.MsoTriState.msoTrue;
        oTxtShape.TextEffect.Alignment = Microsoft.Office.Core.MsoTextEffectAlignment.msoTextEffectAlignmentCentered;
    
        iRows = oNpsMain.CountryCount + 1;
        iColumns = 2;
    
        // adding table
        oShape = pptSlide.Shapes.AddTable(iRows, iColumns, 500, 110, 160, 120);
        oShape.Table.ApplyStyle("{5940675A-B579-460E-94D1-54222C63F5DA}");
        oShape.Table.Cell(1, 1).Merge(oShape.Table.Cell(1, 2));
    
        // adding rows to table
        var countryData = oNpsMain.GetDataCountryWise(CountryCode);
        foreach (var data in countryData)
        {
    	oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Text = data.NPSDate.Value.ToString("dd/MM/yyyy");
    	oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Name = "Arial";
    	oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Size = 10;
    
    
    	oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Text = (data.NPSAmount.ToString().StartsWith("0") ? "0%" : (data.NPSAmount.ToString() + "0%"));
    	oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Name = "Arial";
    	oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Size = 10;
    	row++;
        }
    
        oShape.Top = 100;
        oShape.Left = 30;
    
        // adding image
        oPicShape = pptSlide.Shapes.AddPicture(@"D:\test\chart.png",
    	Microsoft.Office.Core.MsoTriState.msoFalse,
    	Microsoft.Office.Core.MsoTriState.msoTrue, 200, 100, 500, 300);
    }

    can i use Parallel.ForEach instead of regular foreach to add slides simultaneously ?

    Parallel.ForEach(list_lines, line =>
    {
        //Your stuff
    });

    if i use Parallel.ForEach then any race condition will occur ?

    Parallel.ForEach spawn multiple thread ?

    please refactor my above code just to show how could i add all slides simultaneously instead of one after one.

    here is one link on parallel foreach for file operation simultaneously https://msdn.microsoft.com/en-us/library/dd460720%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    thanks



    • Edited by Mou_kolkata Friday, February 10, 2017 9:44 AM
    • Moved by CoolDadTx Friday, February 10, 2017 3:00 PM Office related
    Friday, February 10, 2017 9:38 AM

Answers

  • Hi,

    AFAIK, Office is single thread and does not support multiple threads.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, February 13, 2017 8:54 AM
    Monday, February 13, 2017 6:00 AM
    Moderator
  • Hi,

    I use code below to add multiple slides based on shape count in Slide1.

                Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShape10pointStar, 100, 100, 100, 100);
                Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShape12pointStar,200,200,100,100);
                Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShape16pointStar, 300, 300, 100, 100);
                //foreach (PowerPoint.Shape shp in Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes)
                //{
                //    PowerPoint.Slide oSlide = Globals.ThisAddIn.Application.ActivePresentation.Slides.Add(Globals.ThisAddIn.Application.ActivePresentation.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                //}
                Parallel.ForEach(Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.Cast<Object>(), item=>
                {
                    PowerPoint.Slide oSlide = Globals.ThisAddIn.Application.ActivePresentation.Slides.Add(Globals.ThisAddIn.Application.ActivePresentation.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                });

    There is no error. But it makes no sense because Office is single threaded, so the process would be in the same thread.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Wednesday, February 15, 2017 10:11 AM
    Tuesday, February 14, 2017 9:07 AM
    Moderator

All replies

  • still got no answer or guidance.
    Sunday, February 12, 2017 9:47 AM
  • Hi,

    AFAIK, Office is single thread and does not support multiple threads.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, February 13, 2017 8:54 AM
    Monday, February 13, 2017 6:00 AM
    Moderator
  • if i use parallel.foreach to add slides to PowerPoint then any problem will occur ? if yes then tell me what kind of problem may occur for parallel.foreach to add slides to PowerPoint simultaneously. thanks
    Monday, February 13, 2017 8:54 AM
  • Hi,

    I use code below to add multiple slides based on shape count in Slide1.

                Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShape10pointStar, 100, 100, 100, 100);
                Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShape12pointStar,200,200,100,100);
                Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShape16pointStar, 300, 300, 100, 100);
                //foreach (PowerPoint.Shape shp in Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes)
                //{
                //    PowerPoint.Slide oSlide = Globals.ThisAddIn.Application.ActivePresentation.Slides.Add(Globals.ThisAddIn.Application.ActivePresentation.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                //}
                Parallel.ForEach(Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.Cast<Object>(), item=>
                {
                    PowerPoint.Slide oSlide = Globals.ThisAddIn.Application.ActivePresentation.Slides.Add(Globals.ThisAddIn.Application.ActivePresentation.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                });

    There is no error. But it makes no sense because Office is single threaded, so the process would be in the same thread.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Wednesday, February 15, 2017 10:11 AM
    Tuesday, February 14, 2017 9:07 AM
    Moderator