Re: Macro - Paste pic to powerpoint
Hi VBA_Wizard,
I used Activesheet.Cells.Select because I have hidden columns and rows and they do not get selected when using this code. These vary from file to file but the procedure is the same - copy activesheet visible range and paste as picture. So the issue of the picture being small in a grey tile does not arise.
The odd part is if I replace the code to create a New PPT instead of Opening one I already made - it is able to paste the picture in the slide.
I did select the reference manually because for some reason the vba code is not enabling it automatically.
Heres the code that creates a New PPT
Code
Sub New_PPT_Pic()
'Activate Powerpoint references
On Error Resume Next
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files\Microsoft OfficeXP\Office10\MSPPT.OLB"
On Error Resume Next
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office11\MSPPT.OLB"
On Error Resume Next
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office12\MSPPT.OLB"
On Error Resume Next
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office14\MSPPT.OLB"
'==================================================================================================================
'Copy range from main sheet as pic
Range("A1").Select
ActiveSheet.Cells.Select
Selection.CopyPicture
'Paste as pic in new sheet called ppt and reisze pic
Worksheets.Add.Name = "pptpic"
Range("A1").Select
ActiveSheet.PasteSpecial Picture
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Width = 650
Selection.ShapeRange.Height = 315
Selection.ShapeRange.Left = 0
Selection.shaperang.Top = 0
'Give pic white background
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'Copy the picture
Selection.Copy
'==================================================================================================================
'Open new Powerpoint
Dim NewPPT As PowerPoint.Application
Set NewPPT = CreateObject("PowerPoint.Application")
NewPPT.Visible = msoTrue
'Add presentation if not there
Dim NewPresenation As PowerPoint.Presentation
If NewPPT.Presentations.Count = 0 Then
Set NewPresentation = NewPPT.Presentations.Add
Else
Set NewPresentation = NewPPT.ActivePresentation
End If
'Add title slide if not there
Dim NewSlide As PowerPoint.Slide
If NewPresentation.Slides.Count = 0 Then
Set NewSlide = NewPresentation.Slides.Add(1, ppLayoutTitle).Select
Set NewSlide = NewPresentation.Slides.Add(2, ppLayoutTitleOnly).Select
Else
ActivePresentation.Slides.Range.Select
End If
'Paste pic
NewPPT.ActivePresentation.Slides(2).Select
NewPPT.ActiveWindow.View.Paste
'==================================================================================================================
'Save Powerpoint
Dim ppapp As Object
Set ppapp = GetObject(, "Powerpoint.Application")
ppapp.FileDialog(msoFileDialogSaveAs).Show
'Delete PPT sheet
On Error Resume Next
Application.DisplayAlerts = False
Sheets("pptpic").Delete
Application.DisplayAlerts = True
'Close Excel
ActiveWorkbook.Save
End Sub
Display More