Hi there,
I've found and modified a code that I use to copy a certain range in Excel (which includes some charts) and paste it into powerpoint, to be used as part of a toolbox. This works very well and is extremely useful.
However; the quality of the plots becomes quite low when I paste the range as a bitmap or picture (the only two options I can get to work).
It seems like this is a somewhat known problem (possibly related to the screen resolution), but I've yet to find a solution.
I figure that using an enhanced metafile paste or something might help, but can anyone provide any input on this? It would be EXTREMELY helpful!
The full VBA code I use is listed below.
- Martin
__________________________________________________
I've found and modified a code that I use to copy a certain range in Excel (which includes some charts) and paste it into powerpoint, to be used as part of a toolbox. This works very well and is extremely useful.
However; the quality of the plots becomes quite low when I paste the range as a bitmap or picture (the only two options I can get to work).
It seems like this is a somewhat known problem (possibly related to the screen resolution), but I've yet to find a solution.
I figure that using an enhanced metafile paste or something might help, but can anyone provide any input on this? It would be EXTREMELY helpful!
The full VBA code I use is listed below.
- Martin
__________________________________________________
Code:
Public Enum PasteFormat
xl_Link = 0
xl_HTML = 1
xl_Bitmap = 2
End Enum
Sub Copy_Paste_to_PowerPoint(ByRef ppApp As Object, ByRef ppSlide As Object, ByVal ObjectSheet As Worksheet, _
ByRef PasteObject As Object, Optional ByVal Paste_Type As PasteFormat)
' Modified version of code originally posted here:
' http://www.vbaexpress.com/kb/getarticle.php?kb_id=370
' Modified by : Krishnakumar @ ExcelFox.com
' Used Late binding so that no issues when users have multiple Excel version
Dim PasteRange As Boolean
Dim objChart As ChartObject
Dim lngSU As Long
Dim NewS As Object
Select Case TypeName(PasteObject)
Case "Range"
If Not TypeName(Selection) = "Range" Then Application.Goto PasteObject.Cells(1)
PasteRange = True
Case "Chart": Set objChart = PasteObject.Parent
Case "ChartObject": Set objChart = PasteObject
Case Else
MsgBox PasteObject.Name & " is not a valid object to paste. Macro will exit", vbCritical
Exit Sub
End Select
With Application
lngSU = .ScreenUpdating
.ScreenUpdating = 0
End With
ppApp.ActiveWindow.View.GotoSlide ppSlide.SlideNumber
On Error GoTo -1: On Error GoTo 0
DoEvents
'ppApp.ActiveWindow.ViewType = ppViewSlide
If PasteRange Then
If Paste_Type = xl_Bitmap Then
'//Paste Range as Picture
PasteObject.CopyPicture , Format:=xlPicture
ppSlide.Select
ppSlide.Shapes.Paste.Select
ElseIf Paste_Type = xl_HTML Then
'//Paste Range as HTML
PasteObject.Copy
ppSlide.Shapes.PasteSpecial(8, Link:=1).Select 'ppPasteHTML
ElseIf Paste_Type = xl_Link Then
'//Paste Range as Linked
PasteObject.Copy
ppSlide.Shapes.PasteSpecial(0, Link:=1).Select 'ppPasteDefault
End If
Else
If Paste_Type = xl_Link Then
'//Copy & Paste Chart Linked
objChart.Chart.ChartArea.Copy
ppSlide.Shapes.PasteSpecial(Link:=True).Select
Else
'//Copy & Paste Chart Not Linked
objChart.Chart.CopyPicture Appearance:=1, Size:=1, Format:=2
ppSlide.Shapes.Paste.Select
End If
End If
'//Define size:
ppApp.ActiveWindow.Selection.ShapeRange.Height = 380
'ppApp.ActiveWindow.Selection.ShapeRange.Width =
'//Center pasted object in the slide
ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
ppApp.ActiveWindow.Selection.ShapeRange.Top = 110
With Application
.CutCopyMode = False
.ScreenUpdating = lngSU
End With
AppActivate ("Microsoft Excel")
End Sub
Sub kTest()
Dim ppApp As Object
Dim ppSlide As Object
Dim ppSlideRange As Object
Dim ppPres As Object
On Error Resume Next
Set ppApp = GetObject(, "Powerpoint.Application")
On Error GoTo 0
If ppApp Is Nothing Then
Set ppApp = CreateObject("Powerpoint.Application")
ppApp.Visible = True
ppApp.presentations.Add
End If
If ppApp.ActivePresentation.Slides.Count = 0 Then
Set ppSlide = ppApp.ActivePresentation.Slides.Add(1, 12) 'ppLayoutBlank
Else
'ppApp.ActivePresentation.Slides.Add ppApp.ActivePresentation.Slides.Count + 1, 12
'Set ppSlide = ppApp.ActivePresentation.Slides(ppApp.ActivePresentation.Slides.Count)
Set ppSlide = ppApp.ActivePresentation.Slides(ppApp.ActiveWindow.Selection.SlideRange.SlideIndex)
End If
Set ppPres = ppApp.ActivePresentation
ppApp.ActiveWindow.ViewType = 1
'//or you could specify the slide number. e.g. for the second slide
'Set ppSlide = ppApp.ActivePresentation.Slides(2)
'Copy_Paste_to_PowerPoint ppApp, ppSlide, Sheet1, Sheet1.ChartObjects(1).Chart, xl_Bitmap
'//Range
'Worksheets("Overblik").Select
Copy_Paste_to_PowerPoint ppApp, ppSlide, Worksheets("Overblik (hovedkategori)"), Worksheets("Overblik (hovedkategori)").Range("B24:N61"), xl_Bitmap
Set ppSlide = Nothing
Set ppApp = Nothing
End Sub