swapna panthangi
asked on
Error :Macro to Copy from Excel Worksheets and paste to specific Powerpoint slides
I'm trying to copy the usedrange of a excel worksheet and paste it to as a image in a powerpoint Template of 4th slide and from there on it should move to the next slides and copy the remaining worksheets to the next further slides. I've 20 worksheets in a workbook with different data and with different used Range with 40 slides in a template of a powerpoint presentation. So, it is pasting the data in 4th slide but not adjusting it to the slide as it is throwing the error as
Object Doesn't support this property or method a Runtime Eror 438
So, I Need a macro to loop through all the sheets and if the usedrange is not fitting in the current slide then it should automatically split and go to next slide and then split and paste the remaining data and even if the data is more than that slide also then it should again go to next slide and split and paste the data. Likewise it should automatically paste all the remaining worksheets usedrange to slides and adjust its position according to the data.
I'm attaching sample workbook with 3 worksheets with same data as of now. I do have a powerpoint template of around 40 slides in which it should be started from 4th slide to paste to powerpoint from worksheet.
I'm also providing the code which is working currently . Any Suggestions are highly appreciated!!!
The macro should work with Excel/Powerpoint 2010 or latest
Please provide me the solution for this.
Thank you in advance!!
sample-data.xlsx
Object Doesn't support this property or method a Runtime Eror 438
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myshape = PPslide.Shapes(PPslide.Shapes.Count)
So, I Need a macro to loop through all the sheets and if the usedrange is not fitting in the current slide then it should automatically split and go to next slide and then split and paste the remaining data and even if the data is more than that slide also then it should again go to next slide and split and paste the data. Likewise it should automatically paste all the remaining worksheets usedrange to slides and adjust its position according to the data.
I'm attaching sample workbook with 3 worksheets with same data as of now. I do have a powerpoint template of around 40 slides in which it should be started from 4th slide to paste to powerpoint from worksheet.
I'm also providing the code which is working currently . Any Suggestions are highly appreciated!!!
Private Sub CommandButton2_Click()
Dim PP As PowerPoint.Application
Dim PPpres As Object
Dim PPslide As Object
Dim PpTextbox As PowerPoint.Shape
Dim SlideTitle As String
Dim SlideNum As Integer
Dim WSrow As Long
Dim Sh As Shape
Dim Rng As Range
Dim myshape As Object
Dim myobject As Object
'Open PowerPoint and create new presentation
Set PP = GetObject(class, "PowerPoint.Application")
PP.Visible = True
Set PPpres = PP.Presentations.Open("\\C:\Users\EOL EOS All Domains_Template.pptx")
m = 4
'Specify the chart to copy and copy it
For Each WS In Worksheets
If (WS.Name) <> "EOS" Then
ThisWorkbook.Worksheets(WS.Name).Activate
ThisWorkbook.ActiveSheet.UsedRange.CopyPicture
lastrow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
'Copy Range from Excel
Set Rng = ThisWorkbook.ActiveSheet.Range("A1:I" & lastrow)
'Copy Excel Range
Rng.Copy
For k = m To 45
'Exit For
'Slidecount = PPpres.Slides.Count
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (k)
'Paste to PowerPoint and position
Set PPslide = PPpres.Slides(k)
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myshape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myshape(1).Left = 38
myshape(1).Top = 152
'Add the title to the slide
SlideTitle = "Out of Support, " & WS.Name & " "
Set PpTextbox = PPslide.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 20, PPpres.PageSetup.SlideWidth, 60)
PPslide.Shapes(1).TextFrame.TextRange = SlideTitle
'slidecount = PPpres.Slides.Count
'Set PPslide = PPpres.Slides.Add(slidecount + 1, ppLayoutTitle)
'Make PowerPoint Visible and Active
PP.Visible = True
PP.Activate
'Clear The Clipboard
Application.CutCopyMode = True
m = m + 1
Exit For
Next k
End If
Next WS
End Sub
The macro should work with Excel/Powerpoint 2010 or latest
Please provide me the solution for this.
Thank you in advance!!
sample-data.xlsx
ASKER
Hi Neil,
Thank you so much for the solution.. I think this would really help me out with the solution what i need exactly. But when i tried using the code it is throwing error . So , can you please help me out with this even.
I've a question here that when i'm trying to execute the code while debugging it is throwing error in this lines of code.
Run time Error 1004 Application defined or object defined error
Can you please let me know if something needs to change from my side.
Thank you in Advance!!!
Thank you so much for the solution.. I think this would really help me out with the solution what i need exactly. But when i tried using the code it is throwing error . So , can you please help me out with this even.
I've a question here that when i'm trying to execute the code while debugging it is throwing error in this lines of code.
Run time Error 1004 Application defined or object defined error
'resize copy area to fit slide
While rChunk.Width > iMaxWidth
Set rChunk = rChunk.Resize(rChunk.Rows.Count, rChunk.Columns.Count - 1)
Wend
Can you please let me know if something needs to change from my side.
Thank you in Advance!!!
hmm.. that looks like you have no text area on the default slide, so imaxwidth is maybe set to zero.
have you tried setting the max width manually?
also possible is that there is a worksheet with zero usedRange.
Do you want to post your workbook here?
have you tried setting the max width manually?
also possible is that there is a worksheet with zero usedRange.
Do you want to post your workbook here?
ASKER
Hello Neil,
I'm trying in the same workbook which you have posted here i.e sample-data-PP.xlsm.
I tried setting maxwidth manually..But even I could see throwing the same error..
Can you please check on this and provide me the solution for this..
Again attaching the same for your Reference.
sample-data-PP.xlsm
I'm trying in the same workbook which you have posted here i.e sample-data-PP.xlsm.
I tried setting maxwidth manually..But even I could see throwing the same error..
Can you please check on this and provide me the solution for this..
Again attaching the same for your Reference.
sample-data-PP.xlsm
Can you post your powerpoint file too? I just ran the file you uploaded and it generated 196 slides without a problem. See attached Powerpoint Pres.
Also wondering if you are clicking on Powerpoint while the routine is running, which might cause a 1004 error at the line
I would delete that line, since it is actually not necessary and may be complicating things.
ppTest.pptx
Also wondering if you are clicking on Powerpoint while the routine is running, which might cause a 1004 error at the line
rChunk.Select
.I would delete that line, since it is actually not necessary and may be complicating things.
ppTest.pptx
Also, just to check. The code IS opening your powerpoint file, right?
the file address ""\\C:\Users\EOL EOS All Domains_Template.pptx" looks odd. Or is it a server address, with a C: drive on the server?
the file address ""\\C:\Users\EOL EOS All Domains_Template.pptx" looks odd. Or is it a server address, with a C: drive on the server?
ASKER
Thank you very much Neil for your Quick Response.
As I see in the PPT there are columns splitting in each slide whereas I need the Rows to be split in the slide when there are more rows which doesn't fit in the slide.
I'm providing you the sample PPT and sample Data for your reference.
As my post says that I've 20-25 worksheets in a workbook each with different data with different used range but the column header remains same for every Worksheet. So, when I copy the used range in a worksheet if that contains less records which fits in the slide then that copies the header as well but when there are more than the records that doesn't fit in the slide then it should split those rows (For EX: till 25 rows) then from there on it should paste next 25 rows in the next slide with header and so on till the used range is completely fit in the next further slides and then should go to next worksheet and paste that used range in the next slide . After this if there are any worksheets with more rows which doesn't fit to the slide then it should again split and continue the same till the usedrange is fit completely in the slide.
I've given sample data here in the PPT as well as in the Workbook.
I'm changing the path in VBA as I've given sample path..
the file address ""\\C:\Users\EOL EOS All Domains_Template.pptx" looks odd. Or is it a server address, with a C: drive on the server?
Can you also explain this code how is this working here.
Thank you in Advance!!
PPT_ExpertsExchange.pptx
SampleData_ExpertsExchange.xlsx
As I see in the PPT there are columns splitting in each slide whereas I need the Rows to be split in the slide when there are more rows which doesn't fit in the slide.
I'm providing you the sample PPT and sample Data for your reference.
As my post says that I've 20-25 worksheets in a workbook each with different data with different used range but the column header remains same for every Worksheet. So, when I copy the used range in a worksheet if that contains less records which fits in the slide then that copies the header as well but when there are more than the records that doesn't fit in the slide then it should split those rows (For EX: till 25 rows) then from there on it should paste next 25 rows in the next slide with header and so on till the used range is completely fit in the next further slides and then should go to next worksheet and paste that used range in the next slide . After this if there are any worksheets with more rows which doesn't fit to the slide then it should again split and continue the same till the usedrange is fit completely in the slide.
I've given sample data here in the PPT as well as in the Workbook.
I'm changing the path in VBA as I've given sample path..
the file address ""\\C:\Users\EOL EOS All Domains_Template.pptx" looks odd. Or is it a server address, with a C: drive on the server?
Can you also explain this code how is this working here.
'resize copy area to fit slide
While rChunk.Width > iMaxWidth
Set rChunk = rChunk.Resize(rChunk.Rows.Count, rChunk.Columns.Count - 1)
Wend
iChunkWidth = rChunk.Column + rChunk.Columns.Count
While rChunk.Height > iMaxHeight
Set rChunk = rChunk.Resize(rChunk.Rows.Count - 1, rChunk.Columns.Count)
Wend
iChunkHeight = rChunk.Column + rChunk.Columns.Count
Thank you in Advance!!
PPT_ExpertsExchange.pptx
SampleData_ExpertsExchange.xlsx
That piece of code works as follows:
iMaxWidth and iMaxHeight are determined earlier in the code by looking at the size of the "text" area on the slide (the assumption is you have a header (shape 1) and a text area (shape 2).
The code starts by setting the range rChunk to cover the whole of the UsedRange on each worksheet.
Then if the width of rChunk is greater than iMaxwidth, the chunk size is reduced by one Excel column until it will fit on the PPT slide.
The "while" statement checks whether the chunk is wider than iMaxWidth and if it is, it repeatedly resizes the chunk to have one fewer column. Then it calculates the number of columns in the chunk and stores that value in iChunkWidth to make it easy to move from one chunk to the next later in the code.
The second piece of the code, with iMaxHeight, does the same re-sizing operation with the height of the chunk.
I am trying to understand your explanation of exactly what you want meanwhile.. Do you want the code only to split into row chunks? In other words to resize the pasted image to fit on the slide, no matter how many columns there are?
iMaxWidth and iMaxHeight are determined earlier in the code by looking at the size of the "text" area on the slide (the assumption is you have a header (shape 1) and a text area (shape 2).
Set PPMain = PPslide.Shapes(2)
'get maximum width and height for image based on size of text area
iMaxWidth = PPMain.Width
iMaxHeight = PPMain.Height
The code starts by setting the range rChunk to cover the whole of the UsedRange on each worksheet.
Then if the width of rChunk is greater than iMaxwidth, the chunk size is reduced by one Excel column until it will fit on the PPT slide.
'resize copy area to fit slide
While rChunk.Width > iMaxWidth
Set rChunk = rChunk.Resize(rChunk.Rows.Count, rChunk.Columns.Count - 1)
Wend
iChunkWidth = rChunk.Column + rChunk.Columns.Count
The "while" statement checks whether the chunk is wider than iMaxWidth and if it is, it repeatedly resizes the chunk to have one fewer column. Then it calculates the number of columns in the chunk and stores that value in iChunkWidth to make it easy to move from one chunk to the next later in the code.
The second piece of the code, with iMaxHeight, does the same re-sizing operation with the height of the chunk.
I am trying to understand your explanation of exactly what you want meanwhile.. Do you want the code only to split into row chunks? In other words to resize the pasted image to fit on the slide, no matter how many columns there are?
ASKER
Yes Neil,
I want the code for splitting the row chunks. Yes, Irrespective of the columns I need the rows to be split when it doesn't fit in a single slide.
I want the code for splitting the row chunks. Yes, Irrespective of the columns I need the rows to be split when it doesn't fit in a single slide.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I have recommended this question be closed as follows:
Accept: Neil Fleming (https:#a42161312)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
MacroShadow
Experts-Exchange Cleanup Volunteer
I have recommended this question be closed as follows:
Accept: Neil Fleming (https:#a42161312)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
MacroShadow
Experts-Exchange Cleanup Volunteer
Attached is a workbook that I think does what you are looking for, however. The code below works as follows:
Open in new window
And here is the function "startPP" that switches to or starts Powerpoint (your code assumed the application was already open).
Open in new window
The attached workbook also contains a routine called "clearPP" which I wrote for test purposes. This routine opens your presentation, deletes all slides after slide 10, and clears additional shapes and headers from slide 4 onwards.
Hope this is what you wanted. Using your test file, the code creates around 186 slides...
sample-data-PP.xlsm