VBA Copying and Pasting Charts From Powerpoint To Excel
VBA Copying and Pasting Charts From Powerpoint To Excel
Stack Overflow
Questions
Jobsbeta
Tags
Users
Badges
Ask Question
Sign up
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join
them; it only takes a minute:
up
vote0do
wn vote
favorite
Ok, so I'm trying to write some code which will first create about 15 graphs on excel and
then open a template on powerpoint and paste these graphs into each slide. I'm having
multiplt issues though and I can't really seem to find out why. When I run through the
code line by line by pressing f8 it works fine but the second I run the whole macro a few
things happen: 1) It randomly will decide to not paste some graphs on pages 2) When
trying to refer to the pasted graph via code like .shapes(3) or shapes(slide.shapes.count) I
get an error message saying shapes.item: integer 3 out of range 1 to 2 3) I'm using
CommandBars.ExecuteMso ("PasteSourceFormatting") to paste the graphs as I need to
keep the formatting, I can paste the graphs and position them another way but it doesn't
keep the formatting which I need.
Any suggestions?
Here is the code:
Sub PowerPointPresentation()
PPApp.ActiveWindow.View.GotoSlide (4)
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
With PPSlide
.Shapes(1).TextFrame.TextRange.Text = r & " TCV YTD " & Year(Now()) - 1 & " and "
& Year(Now()) & " - by Type"
.Shapes(2).TextFrame.TextRange.Text = "Totals:
" & Year(Now()) - 1 & ":" & i & "
" & Year(Now()) & ":" & j
End With
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
'Slide 5
Worksheets("Pivots").Range("New_TCV_YTD2014[#All]").Copy
PPApp.ActiveWindow.View.GotoSlide (5)
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
With PPSlide
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
End With
Worksheets("Pivots").ChartObjects(3).Select
ActiveChart.ChartArea.Copy
PPApp.ActiveWindow.View.GotoSlide (5)
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
With PPSlide
.Shapes(1).TextFrame.TextRange.Text = r & " New TCV by AM YTD " & Year(Now())
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
End With
' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Sub
excel
vba
powerpoint
lererferler
295
add a comment
1 Answer
activeoldest
up
vote0do
wn vote
votes
Instead of this:
PPApp.ActiveWindow.View.GotoSlide (1) Set PPSlide =
PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
Try this:
Set PPSlide = PPPres.Slides(1)
No need to go to the slide.
When pasting a chart, try:
Dim oSh As Shape With ActivePresentation.Slides(1) Set oSh =
.Shapes.PasteSpecial(ppPasteOLEObject)(1) ' And now you can use oSh to
position/size/otherwise work with the chart End With
And you can probably do this rather than selecting anything in the workbook:
Worksheets("Pivots").ChartObjects(2).Copy
If you don't select stuff (in either app), your code will run somewhere between ten and
forty-eleven bazillion times faster and will be more reliable. And that alone might solve
the other problem; the app may be busy trying to refresh after the select actions you're
asking for and unable to keep up with the other stuff you ask it to do next.
shareimprove this answer
Steve Rindsberg
5,8651818
Thank you so much for the reply, most of this has helped my code enormously. One issue I am having is that when
pasting the charts onto powerpoint with the 'Set oSh =' method is that it works but it also breaks my code and I get an
error message saying 'type mismatch' and despite it actually pasting it won't go past that line. Any ideas? It also doesn't
keep the source formatting, but I'm a bit past trying to get that to work, I think I'll just write a macro within the
powerpoint afterwards to do the formatting lererferler Dec 9 '14 at 8:46
Are you sure you added the (1) at the end of the Set oSh= command? oSh is dimmed as a shape but .PasteSpecial
returns a ShapeRange; the (1) at the end tells PPT to set oSh = the first shape in the returned ShapeRange. Without it
you will definitely get a Type Mismatch error. Steve Rindsberg Dec 9 '14 at 16:33
I was yes but I found a workaround to the issue, I just did
'PPSlide.Shapes.PasteSpecial(DataType:=ppPasteDefault).Select' I'm not entirely sure why but this meant that I could
refer to the object with PPSlide.Shapes(3) and move it around that way lererferler Dec 10 '14 at 13:14
add a comment
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name
Email
Post Your Answer
By posting your answer, you agree to the privacy policy and terms of service.
1 year ago
697 times
1 year ago
iOS Developer
Entwicklungspark AG (Proje
Zrich, Switzerland / relocation
objective-cswift
Related
2
Excel VBA - pasted object into Powerpoint loses selection for manipulation
Hobbit?
question feed
tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback
TECHNOLOGY
LIFE / ARTS
Stack
Overflow
Programme
rs
Server Fault
Super User
Development
Mathematica
WordPress
Information Systems
Electrical
Engineering
Android
Enthusiasts
Information
Security
ExpressionEngine
Answers
Validated
Mi Yodeya
(Judaism)
Design
Compute
Travel
Movies &
Christianity
TV
Arqade
Seasoned
more (13)
SCIENC
English
Science
Salesforce
Development
Geographic
User Experience
Different (Apple)
TeX LaTeX
SharePoint
Ask
Game
Photograph
Drupal Answers
Linux
Ask Ubuntu
Webmasters
Administrators
Unix &
Web
Applications
Database
CULTURE /
RECREATION
(gaming)
Advice (cooking)
Bicycles
Home
Improvement
Personal
Role-playing
Games
more (21)
site design / logo 2015 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.12.22.3120