2
votes

How do I copy a range of cells from Excel to Powerpoint. I want to do the same thing as when you select a range of cells, copy to clipboard and then paste in a slide in powerpoint.

I have tried Range.Copy(missing) and Slide.Shapes.Paste this code but it's not working. It throws an exception like

'Shapes (unknown member) : Invalid request. Clipboard is empty or contains data which may not be pasted here.'

The clipboard does contain data though and googling the error shows that other people are also facing this issue with Excel 2007 which is what I am using.

My code looks like this:

PowerPoint.ApplicationClass app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
PowerPoint.Presentation pptPresentation = app.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);

Excel.Worksheet NewWorksheet = ThisApplication.Sheets[i] as Excel.Worksheet;
// Add a blank slide to the presentation.
PowerPoint.Slide pptSlide = pptPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);
pptSlide.Name = NewWorksheet.Name;

Excel.Range resultRange = NewWorksheet.UsedRange;
resultRange.Copy(missing); 
pptSlide.Shapes.Paste();

Now I am iterating through the excel sheet's cells one by one, creating a table with the right rows and columns in powerpoint and pasting but it is slow and I have to take care of fitting the data into the slide by changing the font size whereas this is done by powerpoint when you paste the entire range.

How do I copy the entire range without iterating through each cell?

Any suggestions?

1

1 Answers

1
votes

What happens if you use:

app.ActiveWindow.View.Paste(); 
// instead of
// pptSlide.Shapes.Paste();