Excel To PPT VBA Coding - excel

The code runs without fault on a clear blank PowerPoint slide, but when i try Ind add multiple images from excel to PPT onto the same slide it loses its set positioning and formatting and goes to full scale on image 2,3 etc. and unable to identify the course.
Please find the code below which is used to run the script
enter code here
'app
' pre
' slide
' shapes
' text frame
' text
Sub ExporttoPPT()
```
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim slde As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim wb As Workbook
Dim rng As Range
Dim vSheet$
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide_No As Long
Dim expRng As Range
Dim adminSh As Worksheet
Dim cofigRng As Range
Dim xlfile$
Dim pptfile$
Application.DisplayAlerts = False
Set adminSh = ThisWorkbook.Sheets("Admin")
Set cofigRng = adminSh.Range("Rng_sheets")
xlfile = adminSh.[excelPth]
pptfile = adminSh.[pptPth]
Application.DisplayAlerts = False
Set wb = Workbooks.Open(xlfile, False, True)
Application.DisplayAlerts = False
Set pre = ppt_app.Presentations.Open(pptfile)
For Each rng In cofigRng
'----------------- set VARIABLES
With adminSh
vSheet$ = .Cells(rng.Row, 4).Value
vRange$ = .Cells(rng.Row, 5).Value
vWidth = .Cells(rng.Row, 6).Value
vHeight = .Cells(rng.Row, 7).Value
vTop = .Cells(rng.Row, 8).Value
vLeft = .Cells(rng.Row, 9).Value
vSlide_No = .Cells(rng.Row, 10).Value
End With
'----------------- EXPORT TO PPT
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
' Stop
pre.Application.Activate
Set slde = pre.Slides(vSlide_No)
'Application.ActiveWindow.Panes(vSlide_No).Activate
slde.Select
slde.Shapes.PasteSpecial ppPasteBitmap 'ppPasteSecial' ppPasteBitmap
Set shp = slde.Shapes(1)
With shp
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
Set shp = Nothing
Set slde = Nothing
Set expRng = Nothing
Application.CutCopyMode = False
Set expRng = Nothing
Next rng
'pre.Save
'pre.Close
Set pre = Nothing
Set ppt_app = Nothing
wb.Close False
Set wb = Nothing
Application.DisplayAlerts = True
End Sub``

Your problem is that you use always the very first shape when you set the position properties: You write Set shp = slde.Shapes(1).
One solution is to use the last shape instead (new created shapes are put at the end of the collection):
slde.Shapes.PasteSpecial ppPasteBitmap
Set shp = slde.Shapes(slde.shapes.count)
Or you use the return value of PasteSpecial. However, the return value is a ShapeRange (a group of shapes). As in your case the group contains only one shape, you can use
Set shp = slde.Shapes.PasteSpecial(ppPasteBitmap)(1)

Related

How do I solve VBA error 9 - out of range?

I found some code on how to copy and paste a tablet from Excel to PPT, but I keep running into the error 9 'out of range'. It's my first time working with VBA, so I'm not too sure how to fix it.
Here is the code:
Sub ExporttoPPT()
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim slde As PowerPoint.slide
Dim shp As PowerPoint.shape
Dim wb As Workbook
Dim rng As Range
Dim chrt As ChartObject
Dim vSheet$
Dim vRange$
Dim vChart$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide_No As Long
Dim ShpCount As Integer
Dim expRng As Range
Dim expChart As ChartObjects
Dim adminSh As Worksheet
Dim configRng As Range
Dim configChart As Range
Dim xlfile$
Dim pptfile$
Application.DisplayAlerts = False
Set adminSh = ThisWorkbook.Sheets("Admin")
Set configRng = adminSh.Range("Rng_Sheets")
Set configChart = adminSh.Range("Chart_Sheets")
xlfile = adminSh.[excelPth]
pptfile = adminSh.[pptPth]
Set wb = Workbooks.Open(xlfile)
Set pre = ppt_app.Presentations.Open(pptfile, msoFalse)
For Each rng In configRng
With adminSh
vSheet$ = .Cells(rng.Row, 6).Value
vRange$ = .Cells(rng.Row, 7).Value
vWidth = .Cells(rng.Row, 8).Value
vHeight = .Cells(rng.Row, 9).Value
vTop = .Cells(rng.Row, 10).Value
vLeft = .Cells(rng.Row, 11).Value
vSlide_No = .Cells(rng.Row, 12).Value
End With
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
Set slde = pre.Slides(vSlide_No)
slde.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoTrue
ShpCount = slde.Shapes.Count
Set shp = slde.Shapes(ShpCount)
With shp
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
Set shp = Nothing
Set slde = Nothing
Set expRng = Nothing
Application.CutCopyMode = False
Set expRng = Nothing
Next rng
pre.Save
pre.Close
Set pre = Nothing
Set ppt_app = Nothing
wb.Close False
Set wb = Nothing
Application.DisplayAlerts = True
End Sub
The VBA debugger says it is to do with the following line:
Sheets(vSheet$).Activate
Any help would be highly appreciated!!!
Thank you!
Oliver
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
Set slde = pre.Slides(vSlide_No)
slde.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoTrue
May not fix your problem but here's a refactored version combining your two loops into one. Missing any error checking such as ensuring worksheets/chartobjects etc actually exist before trying to access them.
'Declare a Type to hold the various configuration details for a copy/paste/move/size step
'All config is stored in one table with columns as indicated below
Type Settings
Type As String 'need one new column in your table for this
Sheet As String 'source worksheet
Source As String 'range address or chartobject name
Width As Double 'settings for sizing/location...
Height As Double
Top As Double
Left As Double
Slide As Long 'destination slide index
End Type
Sub ExporttoPPT()
Dim ppt_app As New PowerPoint.Application
Dim pres As PowerPoint.Presentation, ws As Worksheet
Dim config As Settings, wb As Workbook, adminSh As Workbook, configRng As Range, rw As Range
Application.DisplayAlerts = False
Set adminSh = ThisWorkbook.Sheets("Admin")
Set rw = adminSh.Range("SettingsRow1") '<< first row of settings in the config table
Set wb = Workbooks.Open(adminSh.[excelPth])
Set pres = ppt_app.Presentations.Open(adminSh.[pptPth], msoFalse)
Do While Application.CountA(rw) > 0 'loop each row in the config table while not blank
config = GetSettings(rw) 'get the configuration from this row
Set ws = wb.Sheets(config.Sheet)
With pres.Slides(config.Slide) 'destination slide
Select Case config.Type 'what kind of thing are we copying?
Case "Range"
ws.Range(config.Source).Copy
.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoTrue
Case "Chart"
ws.ChartObjects(config.Source).Copy 'or eg CopyPicture
.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoTrue
End Select
With .Shapes(.Shapes.Count) 'position and size the pasted shape
.Top = config.Top
.Left = config.Left
.Width = config.Width
.Height = config.Height
End With
End With
Set rw = rw.Offset(1, 0)
Loop
'clean up
pres.Save
pres.Close
ppt_app.Quit
wb.Close False
Application.DisplayAlerts = True
End Sub
'populate and return a `Settings` variable from a row
Function GetSettings(rw As Range) As Settings
Dim rv As Settings
rv.Type = rw.Cells(1).Value
rv.Sheet = rw.Cells(2).Value
rv.Source = rw.Cells(3).Value
rv.Width = rw.Cells(4).Value
rv.Height = rw.Cells(5).Value
rv.Top = rw.Cells(6).Value
rv.Left = rw.Cells(7).Value
rv.Slide = rw.Cells(8).Value
GetSettings = rv
End Function

Not able to resize and position shapes in PowerPoint

I am working on a VBA script which copies some ranges from an Excel document to a PowerPoint document. I am able to do that successfully without any errors. However, after copying the range, when I resize and realign the shapes, I am not able to do so. What might I be missing?
I have defined the ranges of the Excel, slide numbers and the main Excel sheet in a separate file. So as of now, I am taking all the values from that separate file.
Option Explicit
Sub ExportToPPT()
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim slide As PowerPoint.slide
Dim shp As PowerPoint.Shape
Dim wb As Workbook
Dim rng As Range
Dim vSheet$
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim expRng As Range
Dim vslidenum As Long
Dim Adminsh As Worksheet
Dim configRng As Range
Dim xlfile$
Dim pptfile$
Application.DisplayAlerts = False
Set Adminsh = ThisWorkbook.Sheets("Admin")
' "RangeLoop" is the loop range where we are defining the sheets
Set configRng = Adminsh.Range("RangeLoop")
xlfile = Adminsh.[ExcelPath]
pptfile = Adminsh.[PPTPath]
Set wb = Workbooks.Open(xlfile)
Set pre = ppt_app.Presentations.Open(pptfile)
wb.Activate
For Each rng In configRng
' Pick values from the Excel sheet --------------------------------
With Adminsh
vSheet$ = .Cells(rng.Row, 2).Value
vRange$ = .Cells(rng.Row, 3).Value
vWidth = .Cells(rng.Row, 4).Value
vHeight = .Cells(rng.Row, 5).Value
vTop = .Cells(rng.Row, 6).Value
vLeft = .Cells(rng.Row, 7).Value
vslidenum = .Cells(rng.Row, 8).Value
End With
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
' Paste values in PowerPoint-----------------------------------------------
Set slide = pre.Slides(vslidenum)
'ppt_app.Activate
slide.Shapes.PasteSpecial ppPasteBitmap
'ppt_app.ActiveWindow.View.PasteSpecial ppPasteOLEObject, msoFalse
'slide.Shapes.PasteSpecial DataType:=ppPasteBitmap, Link:=msoFalse
Set shp = slide.Shapes(1)
With shp
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
Application.CutCopyMode = False
Set shp = Nothing
Set slide = Nothing
' The line below is showing an error (compile error)
'Application.CutCopyMode = False
'Application.CutCopyMode = False
'aPPLICATION.CU
Set expRng = Nothing
Next rng
pre.Save
'pre.Close
Set pre = Nothing
Set ppt_app = Nothing
Set expRng = Nothing
wb.Close False
Set wb = Nothing
Application.DisplayAlerts = True
End Sub
I think you're probably referencing the wrong shape using the constant index 1.
Set shp = slide.Shapes(1)
The shape you inserted will probably be at the end of the list.
Try doing this instead:
Set shp = slide.Shapes(slide.Shapes.Count)
Instead of pasting and then assigning the shape, you can do that in one go...
Here is an example
Set shp = slide.Shapes.PasteSpecial(ppPasteBitmap)
With shp
'~~> Do what you want
End With

Position and sizing issue with copy/paste from Excel to PowerPoint with VBA

I need to copy/paste tables that I have in excel into powerpoint with a VBA command.
I found this video : https://www.youtube.com/watch?v=dIqoXYy_Clg
And it exactly responds to what I want to do, the only difference that I have is that I want all my tables on the same slide.
However when I'm running the sub, the first two tables are correctly positioned and sized but after the third, they all go into the middle of the slide and the width that I applied change too. I've found that they are some problems with positioning when you copy/paste from excel to powerpoint, but I would like to know if there's a way to force the tables after being pasted to be moved and sized as I originally specified them.
Here's the actual code :
Sub ExporttoPPT()
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim vSheet$
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vShape As Double
Dim expRng As Range
Dim Export_PPT_Sh As Worksheet
Dim ConfigRng As Range
Dim xlfile$
Dim pptfile$
Set Export_PPT_Sh = ThisWorkbook.Sheets("Export_PPT")
xlfile = Export_PPT_Sh.[excelPth]
pptfile = Export_PPT_Sh.[pptPth]
Set wb = Workbooks.Open(xlfile)
Set pre = ppt_app.Presentations.Open(pptfile)
Set ConfigRng = Export_PPT_Sh.Range("Rng_Sheets")
For Each rng In ConfigRng
With Export_PPT_Sh
vSheet$ = .Cells(rng.Row, 4).Value
vRange$ = .Cells(rng.Row, 5).Value
vWidth = .Cells(rng.Row, 6).Value
vHeight = .Cells(rng.Row, 7).Value
vTop = .Cells(rng.Row, 8).Value
vLeft = .Cells(rng.Row, 9).Value
vShape = .Cells(rng.Row, 10).Value
End With
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
Set sld = pre.Slides(1)
sld.Shapes.PasteSpecial ppPasteBitmap
Set shp = sld.Shapes(vShape)
With shp
.Width = vWidth
.Height = vHeight
.Top = vTop
.Left = vLeft
End With
Set sld = Nothing
Set shp = Nothing
Set expRng = Nothing
Next rng
Set pre = Nothing
Set ppt_app = Nothing
wb.Close False
Set wb = Nothing
End Sub
I have on my excel sheet a range with all the properties such as width, height etc...
I'm also on excel and powerpoint 2013 if it's relevant.
It's my first post, so I hope that I've been clear enough. Thanks by advance for the future responses.
Thanks to John Korchock I tried to use Placeholders instead of defining the width, heigth etc...
That way, the tables always go as the intended place and size. The code finally looks like this :
Sub ExporttoPPT()
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim vSheet$
Dim vRange$
Dim vPlcHolder As Long
Dim expRng As Range
Dim Export_PPT_Sh As Worksheet
Dim ConfigRng As Range
Dim xlfile$
Dim pptfile$
Set Export_PPT_Sh = ThisWorkbook.Sheets("Export_PPT")
'Path of the PowerPoint template and the excel worbook.
xlfile = Export_PPT_Sh.[excelPth]
pptfile = Export_PPT_Sh.[pptPth]
'Opening the excel and ppt workbooks
Set wb = Workbooks.Open(xlfile)
Set pre = ppt_app.Presentations.Open(pptfile)
Set ConfigRng = Export_PPT_Sh.Range("Rng_Sheets")
'Variables
For Each rng In ConfigRng
'Set Variables for tables
With Export_PPT_Sh
vSheet$ = .Cells(rng.Row, 4).Value
vRange$ = .Cells(rng.Row, 5).Value
vPlcHolder = .Cells(rng.Row, 6).Value
End With
'Export tables to PPT
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
Set sld = pre.Slides(1)
With shp
sld.Shapes.Placeholders(vPlcHolder).Select msoTrue
sld.Shapes.PasteSpecial ppPasteBitmap
End With
Set sld = Nothing
Set shp = Nothing
Set expRng = Nothing
Next rng
Set pre = Nothing
Set ppt_app = Nothing
wb.Close False
Set wb = Nothing
End Sub
It's may be not the most optimized code, but at least it works everytime without goign as the wrong place.
Thank you again for the comments !

Exporting text from Excel to PowerPoint using VBA

I am writing a VBA code to create a PowerPoint from an Excel document. I have the PowerPoint opening and my text exporting, but am running into a syntax issue because I want to export text and not a table, bitmap, picture, etc.
I have tried the following syntax and am running into errors.
First:
slde.Shapes.PasteSpecial ppPasteBitmap
(along with all other possible "ppPaste..." options)
Second:
slde.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=1, Top:=1, Width:=100, Height:=100)
This one gives me errors with the dimensions even when I've copied someone else's working code to test it out.
Is there a better way to export text?
Thank you.
Code:
Sub ExporttoPPT()
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim slde As PowerPoint.Slide
Dim shpe As PowerPoint.shape
Dim wb As Workbook
Dim Rng As Range
Dim vSheet As String
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide_No As Long
Dim expRng As Range
Dim adminSh As Worksheet
Dim cofigRng As Range
Dim xlfile$
Dim pptfile$
Dim pptapp As Object
Application.DisplayAlerts = False
Set adminSh = ThisWorkbook.Sheets("PPTPage")
Set cofigRng = adminSh.Range("Rngsheets")
pptfile = adminSh.[pptpth]
Set wb = ActiveWorkbook
Set pre = ppt_app.Presentations.Open(pptfile)
'---set variables
For Each Rng In cofigRng
With adminSh
vSheet = .Cells(Rng.Row, 3).Value
vRange$ = .Cells(Rng.Row, 4).Value
vWidth = .Cells(Rng.Row, 5).Value
vHeight = .Cells(Rng.Row, 6).Value
vTop = .Cells(Rng.Row, 7).Value
vLeft = .Cells(Rng.Row, 8).Value
vSlide_No = Cells(Rng.Row, 9).Value
Debug.Print vSheet
Debug.Print vRange$
Debug.Print vWidth
Debug.Print vHeight
Debug.Print vTop
Debug.Print vLeft
Debug.Print vSlide_No
End With
'---export to ppt
wb.Activate
Sheets(vSheet).Activate
Set expRng = Sheets(vSheet).Range(vRange$)
expRng.Copy
Set slde = pre.Slides(vSlide_No)
slde.Shapes.PasteSpecial ppPasteBitmap
slde.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=1, Top:=1, Width:=100, Height:=100)
Set shp = slde.Shapes(1)
With shp
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
Set shp = Nothing
Set slde = Nothing
Application.CutCopyMode = False
Set expRng = Nothing
Next Rng
Set pre = Nothing
'wb.Close False
'Set wb = Nothing
Application.DisplayAlerts = True
End Sub

vba - PasteSpecial - Method or data member not found

I have been following this video on youtube Video and I have copied the code that the author of the video shows.
However, when I try to replicate it, I get the following error 'Method or data member not found' and the highlighted segment of the code is '.PasteSpecial'.
Can anyone help me understand why?
I am using the exact same code and input data of the author of the video.
Don't know if it is relevant, but I am running Excel 2016 on a Mac. The following references are activated:
Visual Basics for Applications
Microsoft Excel 14.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Office 14.0 Object Library
Microsoft PowerPoint 14.0 Object Library
Option Explicit
'app
' pre
' slide
' shapes
' text frame
' text
Sub ExporttoPPT()
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Download free codes from http://vbaa2z.blogspot.com
'Subscribe channel: youtube.com/vbaa2z
'Author: L Pamai (vbaa2z.team#gmail.com)
'-----------------------------
Dim ppt_app As New PowerPoint.Application
Dim pre As PowerPoint.Presentation
Dim slde As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim wb As Workbook
Dim rng As Range
Dim vSheet$
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide_No As Long
Dim expRng As Range
Dim adminSh As Worksheet
Dim cofigRng As Range
Dim xlfile$
Dim pptfile$
Application.DisplayAlerts = False
Set adminSh = ThisWorkbook.Sheets("Admin")
Set cofigRng = adminSh.Range("Rng_sheets")
xlfile = adminSh.[excelPth]
pptfile = adminSh.[pptPth]
Set wb = Workbooks.Open(xlfile)
Set pre = ppt_app.Presentations.Open(pptfile)
For Each rng In cofigRng
'----------------- set VARIABLES
With adminSh
vSheet$ = .Cells(rng.Row, 4).Value
vRange$ = .Cells(rng.Row, 5).Value
vWidth = .Cells(rng.Row, 6).Value
vHeight = .Cells(rng.Row, 7).Value
vTop = .Cells(rng.Row, 8).Value
vLeft = .Cells(rng.Row, 9).Value
vSlide_No = .Cells(rng.Row, 10).Value
End With
'----------------- EXPORT TO PPT
wb.Activate
Sheets(vSheet$).Activate
Set expRng = Sheets(vSheet$).Range(vRange$)
expRng.Copy
Set slde = pre.Slides(vSlide_No)
slde.Shapes.PasteSpecial DataType:=ppPasteBitmap
Set shp = slde.Shapes(1)
With shp
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
Set shp = Nothing
Set slde = Nothing
Set expRng = Nothing
Application.CutCopyMode = False
Set expRng = Nothing
Next rng
pre.Save
pre.Close
Set pre = Nothing
Set ppt_app = Nothing
wb.Close False
Set wb = Nothing
Application.DisplayAlerts = True
End Sub
Update
Based on some comments below, the issue might be that the command PasteSpecial is not present in Mac. For instance, when I replace
slde.Shapes.PasteSpecial DataType:=ppPasteBitmap
with
slde.Shapes.Paste DataType:=ppPasteBitmap
or
slde.Shapes.Paste
The code seems to run but then Excel crashes. So I still need some help :)

Resources