hthart
asked on
From VBA in Excel, Copy charts and Ranges and Paste-special into PowerPoint and Word
I found this routine on experts exchange and it works great, very fast.
How do I modify it to:
Copy a chart & paste-link into PowerPoint? Word?
Copy a range & Paste Picture into PowerPoint? Word?
Copy a range & Paste-Link Picture into PowerPoint? Word?
Copy a range and Paste HTML into PowerPoint? Word?
Copy a range and Paste-Link HTML into PowerPoint? Word?
Thanks
Tim
-------------------------- ---------- ---------- -------
Accepted Answer from brettdj
Date: 04/01/2003 07:55PM PST
Grade: A
Accepted Answer
Goto the excel VBE (Alt & F11)
Select Tools - References
tick 'Microsoft PowerPoint 9.0 Object Library' and hit Ok
then Insert - Module
copy and paste code below
Cheers
Dave
Sub exp()
Dim ws As Worksheet
Dim chas As Object
Dim PPApp As Object
Dim ppslide As Object
Set PPApp = CreateObject("PowerPoint.A pplication ") 'create PowerPoint application object
PPApp.Visible = True 'tart PowerPoint (if its not already running)
'set up PowerPoint window and slide layout
PPApp.Presentations.Add 'create a new presentation
PPApp.ActiveWindow.ViewTyp e = ppViewSlide
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
For Each cha In ws.ChartObjects
PPApp.ActivePresentation.S lides.Add PPApp.ActivePresentation.S lides.Coun t + 1, ppLayoutBlank
PPApp.ActiveWindow.View.Go toSlide PPApp.ActivePresentation.S lides.Coun t
Sheets(ws.Name).ChartObjec ts(cha.Nam e).Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
PPApp.ActiveWindow.View.Pa ste
PPApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignCenters, True
PPApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignMiddles, True
'change from Normal View to Slide View
'PPApp.ActiveWindow.Window State = ppWindowMaximized 'maximize Slide View window
Next
Next
End Sub
How do I modify it to:
Copy a chart & paste-link into PowerPoint? Word?
Copy a range & Paste Picture into PowerPoint? Word?
Copy a range & Paste-Link Picture into PowerPoint? Word?
Copy a range and Paste HTML into PowerPoint? Word?
Copy a range and Paste-Link HTML into PowerPoint? Word?
Thanks
Tim
--------------------------
Accepted Answer from brettdj
Date: 04/01/2003 07:55PM PST
Grade: A
Accepted Answer
Goto the excel VBE (Alt & F11)
Select Tools - References
tick 'Microsoft PowerPoint 9.0 Object Library' and hit Ok
then Insert - Module
copy and paste code below
Cheers
Dave
Sub exp()
Dim ws As Worksheet
Dim chas As Object
Dim PPApp As Object
Dim ppslide As Object
Set PPApp = CreateObject("PowerPoint.A
PPApp.Visible = True 'tart PowerPoint (if its not already running)
'set up PowerPoint window and slide layout
PPApp.Presentations.Add 'create a new presentation
PPApp.ActiveWindow.ViewTyp
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
For Each cha In ws.ChartObjects
PPApp.ActivePresentation.S
PPApp.ActiveWindow.View.Go
Sheets(ws.Name).ChartObjec
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
PPApp.ActiveWindow.View.Pa
PPApp.ActiveWindow.Selecti
PPApp.ActiveWindow.Selecti
'change from Normal View to Slide View
'PPApp.ActiveWindow.Window
Next
Next
End Sub
ASKER
Dave,
Thanks! Basically this code works great.
One issue:
I need to paste a range as an unlinked picture into ppt so that the numbers cannot be changed.
The statement : ppSlide.Shapes.PasteSpecia l(ppPasteD efault, link:=False).Select pastes the range in ppt with editable numbers and it behaves like it is HTML. This so even if I omit ", link:=False".
What is the command to copy a range and paste as an unlinked picture in ppt?s
The rest of the original question is: what is the code to copy and paste charts and ranges into WORD with all the options we have discussed above? Basically the same code as above, just modified for pasting into Word.
Thanks in advance.
Tim
Thanks! Basically this code works great.
One issue:
I need to paste a range as an unlinked picture into ppt so that the numbers cannot be changed.
The statement : ppSlide.Shapes.PasteSpecia
What is the command to copy a range and paste as an unlinked picture in ppt?s
The rest of the original question is: what is the code to copy and paste charts and ranges into WORD with all the options we have discussed above? Basically the same code as above, just modified for pasting into Word.
Thanks in advance.
Tim
ASKER
Dave,
Can you modify your code to where it copies and pastes directly into a currently open PowerPoint file? My application is to paste charts and ranges into an existing ppt onto the currently active slide.
I've tried to modify your code, put its not working for me.
Tim
Can you modify your code to where it copies and pastes directly into a currently open PowerPoint file? My application is to paste charts and ranges into an existing ppt onto the currently active slide.
I've tried to modify your code, put its not working for me.
Tim
Hi Tim,
I still can't get my PP or Word to take images from an Excel file, so I'm posting these comments with code untested
Have you tried pasting the range as a picture type, ie maybe
ppSlide.Shapes.PasteSpecia l (ppPasteBitmap)
you may want to try preceding that with
ActiveSheet.Range("a1:c3") .CopyPictu re
As for automtaing Word and checking for an existing instance you could try this code assuming that you have set a reference to the Word Object. Again, the last line is unfortunately untested
Sub ExpWW()
Dim wApp As Word.Application
On Error Resume Next
Set wApp = GetObject(, "Word.Application")
On Error GoTo 0
If wApp Is Nothing Then Set wApp = New Word.Application
If wApp.Documents.Count = 0 Then wApp.Documents.Add
wApp.Visible = True
ActiveSheet.ChartObjects(1 ).Copy
wApp.Selection.PasteAndFor mat wdChart
End Sub
Cheers
Dave
I still can't get my PP or Word to take images from an Excel file, so I'm posting these comments with code untested
Have you tried pasting the range as a picture type, ie maybe
ppSlide.Shapes.PasteSpecia
you may want to try preceding that with
ActiveSheet.Range("a1:c3")
As for automtaing Word and checking for an existing instance you could try this code assuming that you have set a reference to the Word Object. Again, the last line is unfortunately untested
Sub ExpWW()
Dim wApp As Word.Application
On Error Resume Next
Set wApp = GetObject(, "Word.Application")
On Error GoTo 0
If wApp Is Nothing Then Set wApp = New Word.Application
If wApp.Documents.Count = 0 Then wApp.Documents.Add
wApp.Visible = True
ActiveSheet.ChartObjects(1
wApp.Selection.PasteAndFor
End Sub
Cheers
Dave
ASKER
Dave,
I really appreciate the help even though you are handicapped by not being able to test the code. And I think we are very close to having this figured out.
I slightly modified your subroutine as shown below. I added line numbers to the code to aid our discussion.
As you wrote it, it opens a new instance of PowerPoint, pastes the range with a link, and centers the object on the slide. From that perspective it works great.
I am trying to modify the code so that it does not open a new ppt file, it just pastes into an already open and active ppt file.
I am having no luck. No matter how I try to modify the code, above line 12, to prevent a new ppt file from being created, it always gets an error at line 12. It seems that this code only works if a new instance of ppt is opened. How can it be changed?
Sub pptRangePasteSpecialLink()
1 Dim ppApp As PowerPoint.Application
2 Dim ppSlide As PowerPoint.Slide
3 Set ppApp = New PowerPoint.Application
4 With ppApp
5 .Visible = True
6 .Presentations.Add
7 .ActiveWindow.ViewType = ppViewSlide
8 Set ppSlide = .ActivePresentation.Slides .Add(1, ppLayoutBlank)
9 End With
10 Worksheets("Sheet1").Activ ate
11 ActiveSheet.Range("A3:G10" ).Copy
12 ppSlide.Shapes.PasteSpecia l(ppPasteD efault, link:=True).Select
'Center pasted object in the slide
13 ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignCenters, True
14 ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignMiddles, True
End Sub
Also,
The following routine below also works great for simply pasting an unlinked chart into an already opened ppt file, and this is the routine that I am using for this purpose.
The problem I have is that I don't know all the correct copy and paste commmands for this routine to:
Copy chart and paste link into ppt.
Copy a range and paste link into ppt.
Copy a range and paste picture unlinked
Copy a range and paste HTML unlinked
If you know how to make the following routine copy and paste as I described, then my problem is over.
Sub pptChartPastePicture()
Dim ppApp As Object
Set ppApp = CreateObject("PowerPoint.A pplication ") 'create PowerPoint application object
ppApp.ActiveWindow.ViewTyp e = ppViewSlide
Worksheets("Sheet1").Activ ate
ActiveSheet.ChartObjects(1 ).Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
ppApp.ActiveWindow.View.Pa ste
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignCenters, True
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignMiddles, True
End Sub
Dave,
So with either routine, we are very close to having it figured out. If you could modify the first routine to not open a new ppt instance, or provide the right copy and paste commands for the second routine, the problem is solved.
I greatly appreciate your assistance.
Thank you,
Tim
I really appreciate the help even though you are handicapped by not being able to test the code. And I think we are very close to having this figured out.
I slightly modified your subroutine as shown below. I added line numbers to the code to aid our discussion.
As you wrote it, it opens a new instance of PowerPoint, pastes the range with a link, and centers the object on the slide. From that perspective it works great.
I am trying to modify the code so that it does not open a new ppt file, it just pastes into an already open and active ppt file.
I am having no luck. No matter how I try to modify the code, above line 12, to prevent a new ppt file from being created, it always gets an error at line 12. It seems that this code only works if a new instance of ppt is opened. How can it be changed?
Sub pptRangePasteSpecialLink()
1 Dim ppApp As PowerPoint.Application
2 Dim ppSlide As PowerPoint.Slide
3 Set ppApp = New PowerPoint.Application
4 With ppApp
5 .Visible = True
6 .Presentations.Add
7 .ActiveWindow.ViewType = ppViewSlide
8 Set ppSlide = .ActivePresentation.Slides
9 End With
10 Worksheets("Sheet1").Activ
11 ActiveSheet.Range("A3:G10"
12 ppSlide.Shapes.PasteSpecia
'Center pasted object in the slide
13 ppApp.ActiveWindow.Selecti
14 ppApp.ActiveWindow.Selecti
End Sub
Also,
The following routine below also works great for simply pasting an unlinked chart into an already opened ppt file, and this is the routine that I am using for this purpose.
The problem I have is that I don't know all the correct copy and paste commmands for this routine to:
Copy chart and paste link into ppt.
Copy a range and paste link into ppt.
Copy a range and paste picture unlinked
Copy a range and paste HTML unlinked
If you know how to make the following routine copy and paste as I described, then my problem is over.
Sub pptChartPastePicture()
Dim ppApp As Object
Set ppApp = CreateObject("PowerPoint.A
ppApp.ActiveWindow.ViewTyp
Worksheets("Sheet1").Activ
ActiveSheet.ChartObjects(1
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
ppApp.ActiveWindow.View.Pa
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti
ppApp.ActiveWindow.Selecti
End Sub
Dave,
So with either routine, we are very close to having it figured out. If you could modify the first routine to not open a new ppt instance, or provide the right copy and paste commands for the second routine, the problem is solved.
I greatly appreciate your assistance.
Thank you,
Tim
Hi Tim,
I should have expanded more on my Word example as that shows how to check for an open instance. Hopefully this helps
Cheers
Dave
Sub ExpPP()
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'Make the isntance visible
ppApp.Visible = True
'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation
If ppApp.ActivePresentation.S lides.Coun t = 0 Then
Set ppSlide = ppApp.ActivePresentation.S lides.Add( 1, ppLayoutBlank)
Else
Set ppSlide = ppApp.ActivePresentation.S lides(ppAp p.ActivePr esentation .Slides.Co unt)
End If
'Ranges
Worksheets(1).Range("A3:G1 0").Copy
'Range linked
ppSlide.Shapes.PasteSpecia l(ppPasteD efault, Link:=True).Select
'Range as HTML
ppSlide.Shapes.PasteSpecia l(ppPasteH TML).Selec t
'Range as HTML linked
ppSlide.Shapes.PasteSpecia l(ppPasteH TML, Link:=True).Select
'Charts
Worksheets(1).ChartObjects (1).Select
ActiveChart.ChartArea.Copy
'Chart Linked
ppSlide.Shapes.PasteSpecia l(Link:=Tr ue).Select
'Chart Not Linked
ppSlide.Shapes.PasteSpecia l(Link:=Fa lse).Selec t
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignCenters, True
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignMiddles, True
End Sub
I should have expanded more on my Word example as that shows how to check for an open instance. Hopefully this helps
Cheers
Dave
Sub ExpPP()
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'Make the isntance visible
ppApp.Visible = True
'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation
If ppApp.ActivePresentation.S
Set ppSlide = ppApp.ActivePresentation.S
Else
Set ppSlide = ppApp.ActivePresentation.S
End If
'Ranges
Worksheets(1).Range("A3:G1
'Range linked
ppSlide.Shapes.PasteSpecia
'Range as HTML
ppSlide.Shapes.PasteSpecia
'Range as HTML linked
ppSlide.Shapes.PasteSpecia
'Charts
Worksheets(1).ChartObjects
ActiveChart.ChartArea.Copy
'Chart Linked
ppSlide.Shapes.PasteSpecia
'Chart Not Linked
ppSlide.Shapes.PasteSpecia
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti
ppApp.ActiveWindow.Selecti
End Sub
ASKER
Dave,
I made a few changes to the routine to get it to run and a few enhancements.
1. It now adds a slide to the end of the presentation and pastes on that new last slide.
2. I figured out how to paste a range as an unlinked picture and that option is now included
3. Excel kept erroring on the lines:
'Charts
Worksheets(1).ChartObjects (1).Select
ActiveChart.ChartArea.Copy
For some reason, it just didn't like it, so I had to break it up as shown below to get it to work.
4. The following code does not paste a picture of the chart. It doesn't paste anything at all.
'Chart Not Linked
ppSlide.Shapes.PasteSpecia l(Link:=Fa lse).Selec t
But these line do work:
'Copy & Paste Chart Not Linked
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
ppSlide.Shapes.Paste.Selec t
The parameters after CopyPicture are necessary to keep the chart from being the size of the slide.
Here is the latest.
Sub ExpPP()
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'Make the isntance visible
ppApp.Visible = True
'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation
If ppApp.ActivePresentation.S lides.Coun t = 0 Then
Set ppSlide = ppApp.ActivePresentation.S lides.Add( 1, ppLayoutBlank)
Else
ppApp.ActivePresentation.S lides.Add ppApp.ActivePresentation.S lides.Coun t + 1, ppLayoutBlank
ppApp.ActiveWindow.View.Go toSlide ppApp.ActivePresentation.S lides.Coun t
Set ppSlide = ppApp.ActivePresentation.S lides(ppAp p.ActivePr esentation .Slides.Co unt)
End If
'Options for Copy & Paste Ranges and Charts. Pick one
'Options for Copy & Paste Ranges
Worksheets("PowerPoint").R ange("B3:G 9").Copy
'Paste Range linked
ppSlide.Shapes.PasteSpecia l(ppPasteD efault, Link:=True).Select
' 'Paste Range as HTML
ppSlide.Shapes.PasteSpecia l(ppPasteH TML).Selec t
' 'Paste Range as HTML linked
ppSlide.Shapes.PasteSpecia l(ppPasteH TML, Link:=True).Select
'Copy & Paste Range as Picture unlinked
Worksheets("PowerPoint").R ange("B3:G 9").CopyPi cture
ppSlide.Shapes.Paste.Selec t
'Options for Copy and Paste Charts
Worksheets("PowerPoint").A ctivate 'ChartObjects(1).Select
ActiveSheet.ChartObjects(1 ).Select
'Copy & Paste Chart Linked
ActiveChart.ChartArea.Copy
ppSlide.Shapes.PasteSpecia l(Link:=Tr ue).Select
'Copy & Paste Chart Not Linked
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
ppSlide.Shapes.Paste.Selec t
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignCenters, True
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignMiddles, True
End Sub
Dave,
The above routine now works beautifully as is . It is very fast and clean. All my required copy & paste options are available and functioning.
Two last questions before I close out this out:
1. I need the option of pasting onto the currently visible slide of a currently open ppt file. The user may be working in a ppt file and just simply need to paste into the slide he is currently working on.
The code:
Set ppSlide = ppApp.ActivePresentation.S lides(ppAp p.ActivePr esentation .Slides.Co unt)
Sets to the last slide in the presentation.
What is the command to Set ppSlide to the currently visible slide in a previously opened ppt file?
2. Would you like to earn another 500 points by converting this routine to paste into a Word document? If so, say yes and I will post another question for your benefit.
Thanks a million Dave, great work!
Tim
I made a few changes to the routine to get it to run and a few enhancements.
1. It now adds a slide to the end of the presentation and pastes on that new last slide.
2. I figured out how to paste a range as an unlinked picture and that option is now included
3. Excel kept erroring on the lines:
'Charts
Worksheets(1).ChartObjects
ActiveChart.ChartArea.Copy
For some reason, it just didn't like it, so I had to break it up as shown below to get it to work.
4. The following code does not paste a picture of the chart. It doesn't paste anything at all.
'Chart Not Linked
ppSlide.Shapes.PasteSpecia
But these line do work:
'Copy & Paste Chart Not Linked
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
ppSlide.Shapes.Paste.Selec
The parameters after CopyPicture are necessary to keep the chart from being the size of the slide.
Here is the latest.
Sub ExpPP()
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'Make the isntance visible
ppApp.Visible = True
'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation
If ppApp.ActivePresentation.S
Set ppSlide = ppApp.ActivePresentation.S
Else
ppApp.ActivePresentation.S
ppApp.ActiveWindow.View.Go
Set ppSlide = ppApp.ActivePresentation.S
End If
'Options for Copy & Paste Ranges and Charts. Pick one
'Options for Copy & Paste Ranges
Worksheets("PowerPoint").R
'Paste Range linked
ppSlide.Shapes.PasteSpecia
' 'Paste Range as HTML
ppSlide.Shapes.PasteSpecia
' 'Paste Range as HTML linked
ppSlide.Shapes.PasteSpecia
'Copy & Paste Range as Picture unlinked
Worksheets("PowerPoint").R
ppSlide.Shapes.Paste.Selec
'Options for Copy and Paste Charts
Worksheets("PowerPoint").A
ActiveSheet.ChartObjects(1
'Copy & Paste Chart Linked
ActiveChart.ChartArea.Copy
ppSlide.Shapes.PasteSpecia
'Copy & Paste Chart Not Linked
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
ppSlide.Shapes.Paste.Selec
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti
ppApp.ActiveWindow.Selecti
End Sub
Dave,
The above routine now works beautifully as is . It is very fast and clean. All my required copy & paste options are available and functioning.
Two last questions before I close out this out:
1. I need the option of pasting onto the currently visible slide of a currently open ppt file. The user may be working in a ppt file and just simply need to paste into the slide he is currently working on.
The code:
Set ppSlide = ppApp.ActivePresentation.S
Sets to the last slide in the presentation.
What is the command to Set ppSlide to the currently visible slide in a previously opened ppt file?
2. Would you like to earn another 500 points by converting this routine to paste into a Word document? If so, say yes and I will post another question for your benefit.
Thanks a million Dave, great work!
Tim
Tim,
(1) try
Set ppSlide = ppApp.ActiveWindow.View.Sl ide
(2) I'm more than happy to try it - assuming no one else beats me to it
I enjoyed this one, and at last I can see it working on my work pc
Cheers
Dave
(1) try
Set ppSlide = ppApp.ActiveWindow.View.Sl
(2) I'm more than happy to try it - assuming no one else beats me to it
I enjoyed this one, and at last I can see it working on my work pc
Cheers
Dave
ASKER
Dave,
Thanks a bunch.
Below is my final routine for copying and pasting charts and ranges into PowerPoint. It works flawlessly!
If no ppt is opened, it opens one.
If a ppt is currently active, it pastes into it.
It can paste on the currently active slide or append slides to the end and paste.
It can paste charts linked or unlinked
It can paste ranges as a picture linked or unlinked or as HTML linked or unlinked.
Very cool subroutine! This is the subroutine that I want converted to paste into Word. The Word version needs to behave the same way this one does. Now you have a head start.
I will post the following question right now for you to look for:
How to Convert Excel Copy & Paste to PPT routine to Paste into Word.
I will not close this current question until I know that you have found the new question. That way you won't get lost. This is a good routine to keep handy.
Thanks Dave
What an awesome job!
----------Final Routine-------------
Variable names:
Dim SheetName As String
Dim PasteChart As Variant
Dim PasteChartLink As Variant
Dim ChartNumber As Integer
Dim PasteRange As Variant
Dim RangePasteType As Variant
Dim RangeName As String
Dim AddSlidesToEnd As Variant
Sub Copy_Paste_to_PowerPoint()
'Passed parameters
'SheetName - name of sheet in Excel that contains the range or chart to copy
'
'PasteChart -If = True then routine will copy and paste a chart
'PasteChartLink -If = True then Routine will paste chart with Link; if = False then paste chart no link
'ChartNumber -Chart Object Number
'
'PasteRange -If = True then Routine will copy and Paste a range
'RangePasteType -Paste as Picture linked or unlinked, HTML Linked or unlinked. See routine below for exact values
'RangeName -Name of range to copy; "B3:G9", etc.
'AddSlidesToEnd -If = True then appednd slides to end of presentation and paste. If False then paste on current slide.
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'Make the isntance visible
ppApp.Visible = True
'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation
If ppApp.ActivePresentation.S lides.Coun t = 0 Then
Set ppSlide = ppApp.ActivePresentation.S lides.Add( 1, ppLayoutBlank)
Else
If AddSlidesToEnd = True Then
'Appends slides to end of presentation and makes last slide active
ppApp.ActivePresentation.S lides.Add ppApp.ActivePresentation.S lides.Coun t + 1, ppLayoutBlank
ppApp.ActiveWindow.View.Go toSlide ppApp.ActivePresentation.S lides.Coun t
Set ppSlide = ppApp.ActivePresentation.S lides(ppAp p.ActivePr esentation .Slides.Co unt)
ElseIf AddSlidesToEnd = False Then
'Sets current slide to active slide
Set ppSlide = ppApp.ActiveWindow.View.Sl ide
End If
End If
'Options for Copy & Paste Ranges and Charts
If PasteRange = True Then
'Options for Copy & Paste Ranges
If RangePasteType = "Picture - Linked" Then
'Paste Range linked
Worksheets(SheetName).Rang e(RangeNam e).Copy
ppSlide.Shapes.PasteSpecia l(ppPasteD efault, Link:=True).Select
ElseIf RangePasteType = "HTML - Unlinked" Then
' 'Paste Range as HTML
Worksheets(SheetName).Rang e(RangeNam e).Copy
ppSlide.Shapes.PasteSpecia l(ppPasteH TML).Selec t
ElseIf RangePasteType = "HTML - Linked" Then
'Paste Range as HTML linked
Worksheets(SheetName).Rang e(RangeNam e).Copy
ppSlide.Shapes.PasteSpecia l(ppPasteH TML, Link:=True).Select
ElseIf RangePasteType = "Picture - Unlinked" Then
'Copy & Paste Range as Picture unlinked
Worksheets(SheetName).Rang e(RangeNam e).CopyPic ture
ppSlide.Shapes.Paste.Selec t
End If
End If
If PasteChart = True Then
'Options for Copy and Paste Charts
Worksheets(SheetName).Acti vate
ActiveSheet.ChartObjects(C hartNumber ).Select
If PasteChartLink = True Then
'Copy & Paste Chart Linked
ActiveChart.ChartArea.Copy
ppSlide.Shapes.PasteSpecia l(Link:=Tr ue).Select
End If
If PasteChartLink = False Then
'Copy & Paste Chart Not Linked
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
ppSlide.Shapes.Paste.Selec t
End If
End If
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignCenters, True
ppApp.ActiveWindow.Selecti on.ShapeRa nge.Align msoAlignMiddles, True
End Sub
Thanks a bunch.
Below is my final routine for copying and pasting charts and ranges into PowerPoint. It works flawlessly!
If no ppt is opened, it opens one.
If a ppt is currently active, it pastes into it.
It can paste on the currently active slide or append slides to the end and paste.
It can paste charts linked or unlinked
It can paste ranges as a picture linked or unlinked or as HTML linked or unlinked.
Very cool subroutine! This is the subroutine that I want converted to paste into Word. The Word version needs to behave the same way this one does. Now you have a head start.
I will post the following question right now for you to look for:
How to Convert Excel Copy & Paste to PPT routine to Paste into Word.
I will not close this current question until I know that you have found the new question. That way you won't get lost. This is a good routine to keep handy.
Thanks Dave
What an awesome job!
----------Final Routine-------------
Variable names:
Dim SheetName As String
Dim PasteChart As Variant
Dim PasteChartLink As Variant
Dim ChartNumber As Integer
Dim PasteRange As Variant
Dim RangePasteType As Variant
Dim RangeName As String
Dim AddSlidesToEnd As Variant
Sub Copy_Paste_to_PowerPoint()
'Passed parameters
'SheetName - name of sheet in Excel that contains the range or chart to copy
'
'PasteChart -If = True then routine will copy and paste a chart
'PasteChartLink -If = True then Routine will paste chart with Link; if = False then paste chart no link
'ChartNumber -Chart Object Number
'
'PasteRange -If = True then Routine will copy and Paste a range
'RangePasteType -Paste as Picture linked or unlinked, HTML Linked or unlinked. See routine below for exact values
'RangeName -Name of range to copy; "B3:G9", etc.
'AddSlidesToEnd -If = True then appednd slides to end of presentation and paste. If False then paste on current slide.
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'Make the isntance visible
ppApp.Visible = True
'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation
If ppApp.ActivePresentation.S
Set ppSlide = ppApp.ActivePresentation.S
Else
If AddSlidesToEnd = True Then
'Appends slides to end of presentation and makes last slide active
ppApp.ActivePresentation.S
ppApp.ActiveWindow.View.Go
Set ppSlide = ppApp.ActivePresentation.S
ElseIf AddSlidesToEnd = False Then
'Sets current slide to active slide
Set ppSlide = ppApp.ActiveWindow.View.Sl
End If
End If
'Options for Copy & Paste Ranges and Charts
If PasteRange = True Then
'Options for Copy & Paste Ranges
If RangePasteType = "Picture - Linked" Then
'Paste Range linked
Worksheets(SheetName).Rang
ppSlide.Shapes.PasteSpecia
ElseIf RangePasteType = "HTML - Unlinked" Then
' 'Paste Range as HTML
Worksheets(SheetName).Rang
ppSlide.Shapes.PasteSpecia
ElseIf RangePasteType = "HTML - Linked" Then
'Paste Range as HTML linked
Worksheets(SheetName).Rang
ppSlide.Shapes.PasteSpecia
ElseIf RangePasteType = "Picture - Unlinked" Then
'Copy & Paste Range as Picture unlinked
Worksheets(SheetName).Rang
ppSlide.Shapes.Paste.Selec
End If
End If
If PasteChart = True Then
'Options for Copy and Paste Charts
Worksheets(SheetName).Acti
ActiveSheet.ChartObjects(C
If PasteChartLink = True Then
'Copy & Paste Chart Linked
ActiveChart.ChartArea.Copy
ppSlide.Shapes.PasteSpecia
End If
If PasteChartLink = False Then
'Copy & Paste Chart Not Linked
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
ppSlide.Shapes.Paste.Selec
End If
End If
'Center pasted object in the slide
ppApp.ActiveWindow.Selecti
ppApp.ActiveWindow.Selecti
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Keep at it. We will figure it out.
See you on the next question.
Tim
See you on the next question.
Tim
Code written up with some error testing improvements at http://www.vbaexpress.com/kb/getarticle.php?kb_id=370
Credits to Jon Peltier and Tim.
Cheers
Dave
Credits to Jon Peltier and Tim.
Cheers
Dave
ASKER
Thanks Dave
Tim
Tim
There are a few questions there, how about we tackle the PowerPoint ones first. This code assumed that a reference to the PowerPoint object has been set
The first code should work with a link - my powerpoint doesn't display the charts or pictures as I have some sort of problem but there is a linked chart object there
Sub exp()
Dim ppApp As PowerPoint.Application, ppSlide As PowerPoint.Slide
Set ppApp = New PowerPoint.Application
With ppApp
.Visible = True
.Presentations.Add
.ActiveWindow.ViewType = ppViewSlide
Set ppSlide = .ActivePresentation.Slides
End With
ActiveSheet.ChartObjects(1
ActiveChart.ChartArea.Copy
ppSlide.Shapes.PasteSpecia
End Sub
For your range copy request
ActiveSheet.Range("a1:c3")
and then either
ppSlide.Shapes.PasteSpecia
ppSlide.Shapes.PasteSpecia
remove the link:=True portion if you dont want the link
Cheers
Dave