Excel to Powerpoint using VBA

bobaol

Board Regular
Joined
Jun 3, 2002
Messages
195
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
hello, i want vba to copy my value in cell B1 and paste it to a new Powerpoint slide. I have the following; cell A1 has the value c:\tankman.jpg Cell B1 has the value June4th,1989. My Excel file is named Liberty.xlsm. My Powerpoint file is named Freedom.pptm. It is already open. So, this is how I would like it. I want to 1) Insert a new slide in Powerpoint, 2) insert into it the image from the file in A1, 3) insert the value into the slide (anywhere is fine) from A1 4) insert the value into the slide (anywhere is fine) from B1. any help is appreciated. Thanks in advance.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi bobaol,

Lets say we have a few image to insert like below:
Liberty.xlsm
ABC
1C:\image1.jpgText1
2C:\image2.jpgText2
3C:\image3.jpgText3
4C:\image4.jpgText4
5C:\image5.jpgText5
6
Sheet1


Create a macro module in the excel, try the code below:
VBA Code:
Option Explicit

Sub VBA_excel2ppt()
'excel is the host of VBA, put this macro in Excel

Dim PAplication As PowerPoint.Application
Dim Sld As PowerPoint.Slide

Dim DataRange As Range
Dim DataRow As Range
Dim DataColumn As Range

Dim oShape As PowerPoint.Shape
Dim oPicture As PowerPoint.Shape

Set PAplication = New PowerPoint.Application

PAplication.Visible = msoCTrue
PAplication.WindowState = ppWindowMaximized

Set DataRange = ThisWorkbook.Sheets(1).Range("A1:B5") 'change this cell range for your own data range

For Each DataRow In DataRange.Rows
   
    Set Sld = PAplication.ActivePresentation.Slides.AddSlide(PAplication.ActivePresentation.Slides.Count + 1, PAplication.ActivePresentation.SlideMaster.CustomLayouts(1))
    'Add textbox from column B
        Sld.Shapes(1).TextFrame.TextRange.Text = DataRow.Cells(1, 2)
        Sld.Shapes(1).TextFrame.AutoSize = ppAutoSizeShapeToFitText
        Sld.Shapes(1).Top = 0
       
    'Add picture from column A
    Set oShape = Sld.Shapes.AddPicture(DataRow.Cells(1, 1).Value, msoFalse, msoTrue, 1, 1, -1, -1) '(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
   
    'Size Picture, change this as your own preferences
    Set oPicture = Sld.Shapes(Sld.Shapes.Count)
        oPicture.ScaleHeight 1, msoTrue
        oPicture.ScaleWidth 1, msoTrue
        oPicture.LockAspectRatio = True
        oPicture.Width = 300 '300 is arbitrary
 
   'Center picture, change this as your own preferences
    With PAplication.ActivePresentation.PageSetup
        oPicture.Left = (.SlideWidth \ 2) - (oPicture.Width \ 2)
        oPicture.Top = (.SlideHeight \ 2) - (oPicture.Height \ 2)
    End With
   
Next DataRow
   
End Sub

From your developer, VBA, Tools -> Reference, please tick "Microsoft PowerPoint Object Library", like below:
1714634901804.png


Open readily any powerpoint, and run this macro from your excel.
Hope this can help.
Thanks
 
Upvote 0
Solution

Forum statistics

Threads
1,215,944
Messages
6,127,835
Members
449,411
Latest member
adunn_23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top