0% found this document useful (0 votes)
325 views8 pages

VBA Copying and Pasting Charts From Powerpoint To Excel

VBA Copying

Uploaded by

Armen Ayvazyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
325 views8 pages

VBA Copying and Pasting Charts From Powerpoint To Excel

VBA Copying

Uploaded by

Armen Ayvazyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Stack Exchange Inbox Reputation and Badges

sign up log in tourhelp

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:

VBA copying and pasting charts from


powerpoint to excel

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()

Dim PPT As Object


Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
PPT.Presentations.Open filename:="P:\My Documents\CM Presentation Macro\CM
Presentation Template.pptm"
'copy_chart "sheet_name", 2 ' Name of the sheet to copy graph and slide number
the graph is to be pasted in
Dim PPApp As Object
Dim PPPres As Object
Dim PPSlide As Object
Set PPApp = CreateObject("Powerpoint.Application")
'Set PPSlide = CreateObject("Powerpoint.Slide")
Set PPApp = GetObject(, "Powerpoint.Application")
Set PPPres = PPApp.ActivePresentation
'Slide 1
PPApp.ActiveWindow.View.GotoSlide (1)
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
PPSlide.Shapes(1).TextFrame.TextRange.Text = r & " Country Review
YTD " & Year(Now())
'Slide 2
PPApp.ActiveWindow.View.GotoSlide (2)
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
PPSlide.Shapes(1).TextFrame.TextRange.Text = r & " Country Review
YTD " & Year(Now())
'Slide 3
Worksheets("Pivots").ChartObjects(1).Select
ActiveChart.ChartArea.Copy
i = Pivots.Range("G14").Text
j = Pivots.Range("H14").Text
PPApp.ActiveWindow.View.GotoSlide (3)
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 Sector"
.Shapes(2).TextFrame.TextRange.Text = "Totals:
" & Year(Now()) - 1 & ":" & i & "
" & Year(Now()) & ":" & j
End With
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
'Slide 4
Worksheets("Pivots").ChartObjects(2).Select
ActiveChart.ChartArea.Copy
i = Pivots.Range("V14").Text
j = Pivots.Range("W14").Text

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

shareimprove this question

asked Dec 8 '14 at 15:52

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

answered Dec 9 '14 at 3:19

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.

Not the answer you're looking for? Browse other questions


tagged excel vba powerpoint or ask your own question.
asked
viewed
active
Blog

1 year ago
697 times
1 year ago

Bringing Jobs to Stack Overflow

42 votes comment stats

Looking for a job?

iOS Developer
Entwicklungspark AG (Proje
Zrich, Switzerland / relocation
objective-cswift

Software Architect (m/w)


Haufe Gruppe
Freiburg im Breisgau, Deut
devopscloud

Related
2

Paste Excel Chart into Powerpoint using VBA


0

Paste Unlinked Excel Chart to Powerpoint


2

How to copy a chart from Excel to PowerPoint?


0

Excel VBA - pasted object into Powerpoint loses selection for manipulation

Error Pasting Excel Chart into Powerpoint VBA


0

Fornatting a Table in PowerPoint after being pasted from Excel VBA


1

using VBA in Excel to paste into Powerpoint


0

Copy and paste rows from Excel to Powerpoint


0

Automate cut and paste charts into powerpoint from excel


0

Copy/paste Excel charts to PowerPoint and break links

Hot Network Questions


Why do I go through so many philips bits?
How do we know that the cosmic background radiation comes from the early universe?
Why is the Depiction of the Last Alliance in Rivendell faded out in Lord of the Rings but in Color in The

Hobbit?

What is the efficient way to count set bits at a position or lower?


Does (and when how) the lifetime of a LED depend on the PWM frequency
A multiplication table for the Cheela
call it -- what does this term mean? Context: I can't. I'm calling it. I'm calling it
The Complement Cat
How old are Kylo Ren and Rey?
Inductive reactance: frequency vs current. Why current goes to zero as frequency increases?
Is possessive's apostrophe dispensable in any case?
Math.ceil() issue. Am I losing my mind?
Should I invest when in debt?
Proof of the Mazur-Ulam Theorem
Technological applications of light only wormholes
What was the bright light in the SpaceX film today? (12/21/15)
Does an isomorphism of groups that can be written as a direct product induce isomorphisms on the factors?
I want to list all the files in my Linux server which are older than Dec 31, 2014
Is it possible to have a segmentation fault from a reference?
How does the Doctor "read" the sonic screwdriver?
Is it 'more rigorous' to perform definite integrations, rather than indefinite integration while solving ODEs?
Why does having a soul make Angel non-evil compared to evil humans?
How can I avoid this blur during taking indoor party pictures?
Are professors allowed to accuse someone of cheating based on a general increase in test scores?

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

Language & Usage


Skeptics

Fiction & Fantasy


Graphic

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)

Finance & Money


Academia
more (9)

site design / logo 2015 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.12.22.3120

You might also like