I'm trying to write a code to run from excel, and then open an existing excel and powerpoint file from the c drive, and use the data in the worksheet named "Oct18" to update the FIRST table in Slide 2 of the powerpoint.
The following is the code I wrote, but somehow it doesn't work.
May I know if anyone knows how to go about this please ?
Sub WriteText_toPPT_Table()
'Add a reference to Microsoft Powerpoint 12.0 object library
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppShp As PowerPoint.Shape
Dim ppSld As PowerPoint.slide
Dim xlworkbook As Workbook
Dim firstsheet As String
Dim fileDir As String
Dim excelFile As String
firstsheet = "Oct18"
'fileDir = "c:\masterpresentation.pptx"
'excelFile = "c:\masterexcel.pptx
'1) Open powerpoint application
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = msoTrue
Set xlworkbook = Excel.Application.Workbooks.Open(Filename:=excelFile)
'2) opening an existing presentation
Set ppPres = ppApp.Presentations.Open(Filename:=fileDir)
Set exceldir = Excel.Application.Workbooks.Open(Filename:=excelFile)
ppPres.Slides(2).Shapes(1).Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = xlworkbook.Sheets(firstsheet).Cells(4, 12)
End Sub
Related
I am trying to open a powerpoint and then save it ( not save As) and close it. The problem looks like being htat the file isopened in REad-only Mode.
The code is this
Sub Macro()
Dim ObjPPT As PowerPoint.Application
Dim oPresentation As PowerPoint.Presentation
Dim oslide As PowerPoint.Slide
Dim oshape As PowerPoint.Shape
Dim i As Long
Dim opath As String
Set ObjPPT = New PowerPoint.Application
opath = "G:\Kommunikation_Meetings_Actions\Audits\Lean Audits\Lean Action Tracker & Production
Performance (VBA)\Daily KPI.pptx"
ObjPPT.Visible = msoCTrue
Set oPresentation = ObjPPT.Presentations.Open(opath, msoFalse)
'...........
oPresentation.Save
Application.Wait "00:00:05"
oPresentation.Close
End Sub
How do I copy an Excel chart into a PowerPoint slide?
Here is the code I have so far.
'There is a bunch of other stuff defined.
' Just showing what I think are relevant definitions
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim shp As PowerPoint.Shape
Dim sld As PowerPoint.Slide
Dim xlChrt As Excel.ChartObject
Set pptApp = CreateObject("PowerPoint.Application")
'This opens up my PowerPoint file
Set ppPres = pptApp.Presentations.Open(myPath & pptFile)
'This activates the worksheet where my chart is
Workbooks(wb2).Sheets("Sheet 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Copy
'I think that my copying works because after I run the module,
' I have the chart in my clipboard.
'This takes me to slide 2 of my PowerPoint.
Set sld = pptPres.Slides(2)
sld.Shapes.Paste 'But nothing pastes to the slide
I am copying the chart because it is in my clipboard after the module runs.
I successfully reference the PowerPoint slide as later in the code, I edit text boxes on slide 2.
I believe your code should work if you correct the set pptPres statement. This example is a simplified example based on your code:
Option Explicit
Public Sub CopyChart()
Const myPath = "c:\temp\"
Const pptFile = "test.pptx"
Dim pptApp As New PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
' Open the PowerPoint and reference the slide
Set pptPres = pptApp.Presentations.Open(myPath & pptFile)
Set sld = pptPres.Slides(2)
' Copy the Chart
ActiveWorkbook.Sheets("Sheet 1").ChartObjects("Chart 1").Copy
' Paste it into the PowerPoint
sld.Shapes.Paste
End Sub
And the result ... you can see the chart pasted onto slide 2:
UPDATED ANSWER
Chartsheets aren't as functional as embedded charts: https://learn.microsoft.com/en-us/office/vba/api/excel.chart(object)
Here is one option which is small variation of the above which works for chartsheets:
Option Explicit
Public Sub CopyChartSheet()
Const myPath = "c:\temp\"
Const pptFile = "test.pptx"
Dim pptApp As New PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim myChart As Excel.Chart
Dim sld As PowerPoint.Slide
' Open the PowerPoint and reference the slide
Set pptPres = pptApp.Presentations.Open(myPath & pptFile)
Set sld = pptPres.Slides(2)
' Copy the Chart
Set myChart = ActiveWorkbook.Charts("Chart 1")
myChart.CopyPicture
' Paste it into the PowerPoint
sld.Shapes.Paste
End Sub
I am trying to open a excel file, take the ranges as picture and create a ppt our of it. I am not sure what is going wrong in the code below. Can someone help please? The code works well for all other slides coming from other excel file. The moment I opena excel file and switch , it is throwing "subscript out of range error"
Sub GlobalBankPPT()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppShape As PowerPoint.Shape
Dim ppPic As Variant
Dim j As Integer
Dim wkb As Workbook
Dim sh As Worksheet
Dim WS_Count As Integer, i As Integer
Dim mypp As New ExcelToPPt.cPPt
mypp.InitFromTemplate MyTemplateppt:=mytemp, MyOutputppt:=""
Set ppApp = GetObject(, "PowerPoint.Application")
ppApp.Visible = msoTrue
Set ppPres = ppApp.ActivePresentation
Set wkb = Workbooks.Open(Filename:=ThisWorkbook.Worksheets("Index").Range ("FilePath"))
wkb.Activate
WS_Count = wkb.Worksheets.Count
For i = 1 To WS_Count
If Worksheets(i).Name = "Industry" Then
wkb.Activate
Set sh = ActiveSheet
Exit For
End If
Next I
'--------------------------------------------------------------------Slide7
Range(Sheet15.Range("A150"), Sheet15.Range("Q191")).Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ppPres.Slides(7).Select
Set ppPic = ppPres.Slides(7).Shapes.Paste
ppPic.Select
ppPic.Top = 70.24
ppPic.Width = 10.5 * 100
ppPic.Height = ppPic.Width / 3.4
ppPic.Left = 50
end sub
I wanted to open ppt form SharePoint using the VBA, using the command button
if any one knows please help me.
Thanks...
I have similar code...below.. but its giving me the error
Dim PPT As PowerPoint.Application
Dim fileName As Variant
Dim WS1 As Worksheet
Dim rng As Range
Set WS1 = ThisWorkbook.Worksheets("Sheet4")
Set rng = WS1.Range("AA4")
Set PPT = New PowerPoint.Application
fileName = rng
PPT.Visible = True
'PPT.Presentations.Open fileName:="F:\Reports\" & Business_Plan.Value & ".ppt"
PPT.Presentations.Open fileName("AA4")
Assuming the value of "A2" in Sheet 1 is the entire Path+File Name (F:\Reports\FileName.ppt)
Sub openPowerP()
Dim PPT As PowerPoint.Application
Dim fileName As String
Dim WS1 As Worksheet
Set WS1 = ThisWorkbook.Sheets("Sheet1")
fileName = WS1.Range("A2").Value
Set PPT = New PowerPoint.Application
PPT.Presentations.Open fileName
End Sub
The reference to powerpoint is needed of course.
Sub jede_Grafik_nach_PowerPoint()
'Extras - Verweise: Microsoft PowerPoint x.x Object Library
Dim Grafik As Shape
Dim PP As Object
Set PP = CreateObject("Powerpoint.Application")
Dim PP_Datei As PowerPoint.Presentation
Dim PP_Folie As PowerPoint.Slide
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set PP_Datei = PP.Presentations.Open("C:\Users\akaygun\Desktop\test.pptm")
PP.Visible = msoTrue
'Set PP_Datei = PP.ActivePresentation wenn akt. Präsi sein soll
For Each ws In wb.Sheets
If Left(ws.Name, 3) = "MLK" Then
'neue Folie einfügen
PP_Datei.Slides(3).Copy
PP_Datei.Slides.Paste
Set PP_Folie = PP_Datei.Slides(PP_Datei.Slides.Count)
'copypaste
ws.Shapes("Stunden").Copy
PP_Folie.Shapes.Paste
PP_Folie.Shapes("Stunden").Top = 315.1991
PP_Folie.Shapes("Stunden").Left = 22.17449
ws.Shapes("Tage").Copy
PP_Folie.Shapes.Paste
PP_Folie.Shapes("Tage").Top = 10.16945
PP_Folie.Shapes("Tage").Left = -2.806772
End If
Next ws
End Sub
Dear Community,
I am trying to paste diagramms from excel to Powerpoint via VBA automatically.
When running this Sub it always says : "Remote server Computer doesnt exist"
'462'
I already tried to set a New Presentation instead of an Object but it did not help.