I have built a workbook to facilitate the creation of a monthly report presentation I am in charge of. The workbook has some data sheets, some processing sheets and then numbered sheets which contain the charts I need to paste to the corresponding slide. So far, I've built the VBA for opening the PowerPoint template and looping through each excel sheet, and discriminating which sheet names are numeric, and then activating the corresponding slide on the powerpoint template.
Unlike other solutions to similar problems I've found, I'd like to copy all charts from each numbered sheet to each slide at a time, as they are different in shape, quantities and disposition for each sheet/slide. I've mostly only found people copying one chart at a time and pastying as image, which will also not work for me (I need to fine tune data labels and position on the final slide). Any hints as to how could I achieve that?
Here's what my code looks like so far:
Sub CriarSlides()
Dim pptApp As Powerpoint.Application
Dim pptPres As Powerpoint.Presentation
Dim strFileToOpen As Variant
Dim strFileName As String, Hosp As String
Dim datawb As Workbook
Dim xlsCounter As Integer, xlsSlide As Integer
Set datawb = ThisWorkbook
strFileToOpen = Application.GetOpenFilename _
FileFilter:="Powerpoint Files *.pptx (*.pptx),")
If strFileToOpen = False Then
Exit Sub
Else
Set pptApp = New Powerpoint.Application
pptApp.Visible = True
pptApp.Presentations.Open Filename:=strFileToOpen, ReadOnly:=msoFalse, Untitled:=msoTrue
Set pptPres = pptApp.Presentations(1)
End If
For xlsCounter = datawb.Worksheets.Count To 1 Step -1
If IsNumeric(datawb.Worksheets(xlsCounter).Name) Then
xlsSlide = datawb.Worksheets(xlsCounter).Name
' This is the problematic part
Debug.Print xlsSlide
End If
Next xlsCounter
End Sub
With the following modified code you can paste the chart-objects of each sheet in the corresponding slide:
Sub CriarSlides()
Dim pptApp As PowerPoint.Application, pptPres As PowerPoint.Presentation
Dim strFileToOpen As Variant, sh As Worksheet, ch As ChartObject
strFileToOpen = Application.GetOpenFilename(FileFilter:="Powerpoint Files *.pptx (*.pptx),")
If strFileToOpen = False Then Exit Sub
Set pptApp = New PowerPoint.Application
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Open(fileName:=strFileToOpen, ReadOnly:=msoFalse)
For Each sh In ThisWorkbook.Sheets
If IsNumeric(sh.name) Then
For Each ch In sh.ChartObjects
ch.Copy
With pptPres.Slides(CLng(sh.name)).Shapes.Paste
.Top = ch.Top
.Left = ch.Left
.Width = ch.Width
.Height = ch.Height
End With
Next
End If
Next
End Sub
Related
I have been working on automating the copying of editable charts from an excel workbook to a PowerPoint presentation through VBA. I got a lot of help through this link Using VBA to Paste Excel Chart with Data into PowerPoint which has sorted the copy-pasting bit.
Sub CopyChartSlide2()
Application.ScreenUpdating = False
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim cht1 As Excel.ChartObject
Dim Data As Excel.Worksheet
Dim pptcht1 As PowerPoint.Shape
Dim iLoopLimit As Long
Dim OpenPptDialogBox As Object
Dim SlideIndex As Long
Application.ScreenUpdating = False
'Look for existing instance
Set newPowerPoint = CreateObject("PowerPoint.Application")
Set OpenPptDialogBox = newPowerPoint.FileDialog(msoFileDialogOpen)
If OpenPptDialogBox.Show = -1 Then
newPowerPoint.Presentations.Open (OpenPptDialogBox.SelectedItems(1))
End If
Set activeSlide = newPowerPoint.ActivePresentation.Slides(1)
SlideIndex = 1
Set Data = Worksheets("Slide2")
Set cht1 = Data.ChartObjects("Chart1")
cht1.Copy
newPowerPoint.CommandBars.ExecuteMso "PasteExcelChartDestinationTheme"
DoEvents
On Error Resume Next
Do
DoEvents
Set pptcht1 = activeSlide.Shapes(activeSlide.Shapes.Count)
If Not pptcht1 Is Nothing Then Exit Do
iLoopLimit = iLoopLimit + 1
If iLoopLimit > 100 Then Exit Do
Loop
On Error GoTo 0
Debug.Print "iLoopLimit = " & iLoopLimit
With pptcht1
.Left = 103.68
.Top = 84.24
End With
iLoopLimit = 0
AppActivate newPowerPoint.Caption
Set activeSlide = Nothing
Set newPowerPoint = Nothing
Application.ScreenUpdating = True
End Sub
However, when the charts get pasted, it embeds the entire workbook instead of just the worksheet. Since I am working with a workbook of around 20 sheets, and each time a chart is pasted into the presentation, the entire workbook gets embedded and since there are many charts, it makes the PPT heavy and makes the process very slow. Is there a way to only embed the worksheet that is relevant to the chart?
I need to generate multiple powerpoint files by updating chart in 2nd slide on excel data available in each row(dynamic row counts)
I have a excel file with around 1000 rows(count is dynamic every time) and each row is a record, based on 1 row i have created a chart in excel itself which i need to copy in second slide of my existing ppt template. So in this way i need to generate 1000 ppts and save the files based on the name available in same row, can any one help me to resolve this query.
My logic is something like this.
Loop through all the rows
Create sheet
Create chart for first row
Copy paste in ppt fist slide
Then delete the chart or sheet in workbook
Repeat all the step till the end
Below is the code which i tried earlier where in i have created the chart in ppt and linked to the 1st row of my data file, but it only solves half of my problem that i can create only one report not multiple.
Sub Update()
Dim CName, pth
pth = ThisWorkbook.Path
Dim pptPres As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Dim Sld As PowerPoint.Slide
Dim sh As PowerPoint.Shape
Dim wb As Workbook
Dim aLinks As Variant
Dim FName As String
Dim strPptTemplatePath As String
strPptTemplatePath = "C:\Users\DSS1080\Desktop\Business continuity planning\Report Template.pptx"
Application.ScreenUpdating = False
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = msoTrue
Set pptPres = pptApp.Presentations.Open(strPptTemplatePath, untitled:=msoTrue)
NewLink = pth & "\" & ThisWorkbook.Name
pptApp.Activate
For Each Sld In pptPres.Slides
For Each sh In Sld.Shapes
If sh.Type = msoChart Then
sh.Chart.ChartData.Activate
Set wb = sh.Chart.ChartData.Workbook
aLinks = wb.LinkSources(xlExcelLinks)
wb.Sheets(1).Cells(100, 100).Value = aLinks
Oldfile = Cells(100, 100).Value
wb.ChangeLink Name:=Oldfile, NewName:=NewLink, Type:=xlExcelLinks
wb.Sheets(1).Cells(100, 100).Clear
wb.Close False
Set wb = Nothing
sh.Chart.ChartData.Activate
Set wb = sh.Chart.ChartData.Workbook
wb.Close False
Set wb = Nothing
End If
Next
Next
FName = Sheets("Quadrant").Range("C1").Text
CName = Left(strPptTemplatePath, Len(strPptTemplatePath) - 19) & FName
pptPres.SaveAs CName, ppSaveAsDefault
pptPres.Close
Set pptPres = Nothing
pptApp.Quit
Set pptApp = Nothing
Application.ScreenUpdating = True
End Sub
Is there a way such that i can copy and paste multiple charts that are grouped in four as shown below from excel to my existing powerpoint slides 28 and slides 29? The name of the groups are group 16 for the left group, group 17 for the right group. I have tried to use Chrt.CopyPicture but it only copies charts separately to the slides instead of a group like the one outline on the 4 charts shown on the left side of the picture below. By the way, my only code only copies charts individually to slide 28.
Sub ExportChartsTopptSingleWorksheet()
'Declare PowerPoint Variables
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTShape As Object
Dim mySlide, myslide2 As Object
'Declare Excel Variables
Dim Chrt As ChartObject
If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")
On Error GoTo 0
PPTApp.Visible = True
'Create new presentation in the PowerPoint application.
Set PPTPres = PPTApp.Presentations.Open(Filename:="\\fab2crp-nas1\home22\kkang2\Profile\Desktop\myassignment3\mypresentationsample.pptx")
Set mySlide = PPTPres.Slides.Add(28, 1)
'Loop through all the CHARTOBJECTS in the ACTIVESHEET.
For Each Chrt In ActiveSheet.ChartObjects
'Copy the Chart
Chrt.CopyPicture '<------ method copy fail error here
'paste all the chart on to exisitng ppt slide 28
mySlide.Shapes.Paste
Next Chrt
End Sub
Currently, charts are copied individually to ppt slides
Expected
This worked for me.
Sub ExportChartsTopptSingleWorksheet()
Const PER_ROW As Long = 2 'charts per row in PPT
Const T_START As Long = 40 'start chart top
Const L_START As Long = 40 'start chart left
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTShape As Object
Dim mySlide, myslide2 As Object, i As Long
Dim Chrt As ChartObject, T As Long, L As Long
If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")
PPTApp.Visible = True
Set PPTPres = PPTApp.Presentations.Add()
Set mySlide = PPTPres.Slides.Add(1, 1)
i = 0
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Chart.CopyPicture
i = i + 1
'work out the top/left values
T = T_START + (Application.Floor((i - 1) / PER_ROW, 1)) * Chrt.Height
L = L_START + ((i - 1) Mod PER_ROW) * Chrt.Width
With mySlide.Shapes
.Paste
.Item(.Count).Top = T
.Item(.Count).Left = L
End With
Next Chrt
End Sub
I have developed vba code in excel to show all charts in excel to different slides in ppt. But i want the vba code to be implemented in powerpoint instead of excel so that i can create an addin in powerpoint with that macro. I have tried to implement the excel vba code in powerpoint but that doesnot work in ppt. The problem is that it is copying the charts from the excel to the ppt slides.`I have used the following code in ppt but with no success.
Sub Button1()
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.ActivePresentation
Dim xlApp As Object
Dim xlWorkBook As Object
Dim wb As Workbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set wb = xlApp.Workbooks.Open("C:\Users\tonmoy.roy\Desktop\Meeting Files\Monthly Review July 10.xls", True, False)
Dim WAIT As Double
WAIT = Timer
While Timer < WAIT + 10
DoEvents 'do nothing
Wend
wb.Activate
Dim ws As Worksheet
Dim intChNum As Integer
Dim objCh As Object
'Count the embedded charts.
For Each ws In wb.Worksheets
intChNum = intChNum + ws.ChartObjects.Count
Next ws
'Check if there are chart (embedded or not) in the active workbook.
If intChNum + ActiveWorkbook.Charts.Count < 1 Then
MsgBox "Sorry, there are no charts to export!", vbCritical, "Ops"
Exit Sub
End If
'Loop through all the embedded charts in all worksheets.
For Each ws In wb.Worksheets
For Each objCh In ws.ChartObjects
Call pptFormat(objCh.Chart)
Next objCh
Next ws
'Loop through all the chart sheets.
For Each objCh In wb.Charts
Call pptFormat(objCh)
Next objCh
'Show the power point.
pptApp.Visible = True
'Cleanup the objects.
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
'Infrom the user that the macro finished.
MsgBox "The charts were copied successfully to the new presentation!", vbInformation, "Done"
End Sub
Private Sub pptFormat(xlCh As Chart)
'Formats the charts/pictures and the chart titles/textboxes.
Dim chTitle As String
Dim j As Integer
On Error Resume Next
'Get the chart title and copy the chart area.
chTitle = xlCh.ChartTitle.Text
xlCh.ChartArea.Copy
'Count the slides and add a new one after the last slide.
pptSlideCount = pptPres.Slides.Count
Set pptSlide = pptPres.Slides.Add(pptSlideCount + 1, ppLayoutBlank)
'Paste the chart and create a new textbox.
pptSlide.Shapes.PasteSpecial ppPasteJPG
If chTitle <> "" Then
pptSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 12.5, 20, 694.75, 55.25
End If
'Format the picture and the textbox.
For j = 0 To pptSlide.Shapes.Count
With pptSlide.Shapes(j)
'Picture position.
If .Type = msoPicture Then
.Top = 87.84976
.Left = 33.98417
.Height = 422.7964
.Width = 646.5262
End If
'Text box position and formamt.
If .Type = msoTextBox Then
With .TextFrame.TextRange
.ParagraphFormat.Alignment = ppAlignCenter
.Text = chTitle
.Font.Name = "Tahoma (Headings)"
.Font.Size = 28
.Font.Bold = msoTrue
End With
End If
End With
Next j
End Sub
Private Sub pptFormat(xlCh As Chart) should be :
Private Sub pptFormat(xlCh As Excel.Chart).
PowerPoint has a Chart in its Object Model, so you need to change it to explicitly say Excel.Chart
I am assuming you already have the references
If intChNum + ActiveWorkbook.Charts.Count < 1 Then should be:
If intChNum + wb.Charts.Count < 1 Then
Also your variables aren't declared properly as far as I can see in the pptFormat function. Dim them and use Option Explicit in your coding.
Option Explicit helps in long run more than any inconvenience of having to type out decs.
Tonmoy Roy,
You should ask your second question in another thread. But here is some code to have you select a file and get it's name, path or just the entire name/path
Set XLapp = New Excel.Application
'choose the data file
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Select the Data File (Data File.xlsx)."
'clear filters so all file are shown
.Filters.Clear
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
FullName = .SelectedItems(1) 'name and path
End If
End With
fname = Dir(FullName) ' gets just the file name and not the path
XLapp.Visible = True
Set xlWorkBook = XLapp.Workbooks.Open(FullName, False, True) 'Opens the data xlsx file
I want to build a macro that connects our Excel-Data-Sheet with our Reporting-Powerpoint-Presentation.
So I have this named Range ("A") selected and copied.
Then I want to paste the data into a shape in Powerpoint which has the same name as my Range ("A").
Sub SyncWithPPT()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptShape As PowerPoint.Shape
Set pptApp = New PowerPoint.Application
pptApp.Visible = msoTrue
Set pptPres = pptApp.presentations.Open("workingPath")
ActiveWorkbook.Names("A").RefersToRange.Select
Selection.Copy
Set pptShape = pptPres.Slides("anySlide").Shapes("A")
pptShape.Table.cell(1, 1).Shape.TextFrame.TextRange.Paste 'Here it won't paste correctly
End Sub
Everything works just fine, except the pasting. When I paste the selection everything is pasted into cell(1, 1).But I want to copy each cell into a different cell. Like it does when you paste with STRG + V.
Any help would be really appreciated.
This worked for me (Office 2007)...
Sub Tester()
Dim ppt, sld
'presentation is already open...
Set ppt = GetObject(, "powerpoint.application")
Set sld = ppt.activepresentation.slides(1)
ActiveSheet.Range("A1:B2").Copy
sld.Shapes(1).Table.Cell(1, 1).Select
ppt.ActiveWindow.View.Paste
Set sld = Nothing
Set ppt = Nothing
End Sub
'this is how to extract each cell information
'assuming that ppt communication is already done.
Dim n As Integer, j As Integer
Dim ultimaFila As Long
j = 1 'columna
ultimaFila = Range("A65536").End(xlUp).Row
For n = 1 To ultimaFila
pptShape.Table.cell(n, j).Value = Application.Workbooks("Book1").Worksheets("Sheet1").Cells(n, j).Value
Next n
End Sub