Reputation: 87
Quick question hopefully someone can help out here. I'm trying to copy and paste slides from one powerpoint presentation to the next. I've got it currently so that it will copy and paste all the number of slides over to the correct one, but my problem is it is only pasting over the last slide of the presentation over and over. I've tried both for/foreach loops, and yet is just give me the one slide, I'm wondering if it isn't the CommandBars. But I see them being used to reset slides in a for/foreach loop. Any ideas?
public void AppendPPTX(string newContent)
{
int sourceSlideRange = 0;
int targetSlideRange = Application.ActiveWindow.Presentation.Slides.Count;
PowerPoint.Presentation target;
PowerPoint.Presentation source;
try
{
target = Application.ActivePresentation;
source = Application.Presentations.Open(newContent, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse);
sourceSlideRange = source.Slides.Count + 1; //otherwise I was just getting the second to the last slide
for (int i = 1; i < sourceSlideRange; i++)
{
source.Slides[i].Copy();
target.Slides[targetSlideRange].Select();
target.Application.CommandBars.ExecuteMso("PasteSourceFormatting");
}
source.Close();
}
catch (Exception)
{
MessageBox.Show("Error opening PowerPoint, corruption found inside the powerpoint file. " +
Environment.NewLine + "The corrupted file has been deleted." + Environment.NewLine +
"Please attempt to redownload file.",
"Error Opening PowerPoint",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Upvotes: 1
Views: 3843
Reputation: 11
I have used Application.DoEvents()
after PasteSourceFormatting
and it worked fine!
Upvotes: 1
Reputation: 3565
Try saving your PPT after PasteSourceFormatting. I did had the same problem.
Upvotes: 2