I'm slowly getting crazy because of this problem. I'm creating a powerpoint presentation from an excel workbook, where data needs to be filled in. I'm creating multiple slides already with no issues and tackled most problems already.
One of the final things for me to do is copy a chart from excel and pasting it in my ppt. This has worked before, but suddenly it just breaks, it doesnt want to paste the chart anymore.
In my main module I call sub ROI with some required data to continue
Call ROI(PPPRes, Slidestart, language, i)
This is in a seperate Module to keep things clean in the main module
Sub ROI(PPPRes, Slidenumber, language, proposal)
Set pp = CreateObject("PowerPoint.Application")
Dim oPPTShape As PowerPoint.Shape
Dim PPSlide As PowerPoint.Slide
Dim ColumnWidthArray As Variant
Dim i As Integer
'Create a slide on Slidenumber location
Set PPSlide = PPPRes.Slides.Add(Slidenumber, ppLayoutTitleOnly)
PPSlide.Select
PPSlide.Shapes("Title 1").TextFrame.TextRange.Text = Range("Titlename in chosen language")
PPSlide.Shapes.AddTable(3, 3).Select
Set oPPTShape = PPSlide.Shapes("Table 4")
'Filling in data in the table from an excel table. Basic stuff working with a few loops to make this happen
'Changing the width of the created table, column by column
ColumnWidthArray = Array(37, 210, 180)
Set oPPTShape = PPSlide.Shapes("Table 4")
On Error Resume Next
With oPPTShape
For i = 1 To 3
.table.columns(i).width = ColumnWidthArray(i - 1)
Next i
.Top = 180
.Left = 520
.height = 200
End With
'Add a rectangle on the slide
PPSlide.Shapes.AddShape Type:=msoShapeRectangle, Left:=404, Top:=400, width:=153, height:=43
'Copy a picture from excel and paste it in the active slide
Sheets("Shapes").Shapes("ROI_img").Copy
PPSlide.Shapes.Paste.Select
pp.ActiveWindow.Selection.ShapeRange.Left = 800
pp.ActiveWindow.Selection.ShapeRange.Top = 20
'Copy chart from excel (with index number that is linked to "proposal") and then paste onto slide
Sheets("Proposals").Shapes("ChartInvProp" & proposal).Copy
PPSlide.Shapes.Paste.Select
Set oPPTShape = PPSlide.Shapes("ChartInvProp" & proposal)
With PPSlide.Shapes("ChartInvProp" & proposal)
.Left = 20
.Top = 120
.width = 480
.height = 320
End With
end sub
So everything in the code is executed, but most of the time the chart from excel is NOT being pasted onto the slide.
However, if I checked what is in the clipboard by breaking the code just after copying the chart from excel. And I then manually paste whatever is in the clipboard into a Word document I will see the chart. --> The action of copying the chart is being executed, but not the pasting part
If I now continue after the break, the chart will be pasted on the powerpoint somehow. But if I do NOT break the code, and let it run its course, the chart will not be pasted.
Somehow it seems to need more time after copy before it can actually paste the chart. I dont really understand what is happening here.
Sometimes it only pastes Chart 1 in the slide, and when it loops for the second/third/etc... chart it doesnt want to paste it anymore.
It really is very random, and I only see a little bit of structure in it not executing...
This was the solution, using a 'DoEvents' between copy and pasting.
This issue only occurred with Charts made in Excel, if I made the charts into pictures it worked without a problem. But copy/pasting a chart from Excel apparently takes more processing time and was slower than the program run speed. So it would skip from time to time.
Sheets("Proposals").Shapes("ChartInvProp" & proposal).Copy
DoEvents
PPSlide.Shapes.Paste.Select
Got the answer from:
Error in script which copies charts from Excel to PowerPoint
Related
I have a PowerPoint presentation in which I create charts in Excel and then link them into the PowerPoint. There are two ways to do this:
Paste Special > Paste Link > Microsoft Excel Chart Object
Paste > Keep Source Formatting and Link Data / Use Destination Theme and Link Data
I would late like to use VBA to change the source Excel file. To do this, consider the following code:
Private Sub PrintLinks()
Dim pptPresentation As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape
Set pptPresentation = ActivePresentation
For Each pptSlide In pptPresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoChart Or pptShape.Type = msoLinkedOLEObject Or pptShape.Type = msoLinkedChart Then
Debug.Print pptShape.LinkFormat.SourceFullName
pptShape.LinkFormat.SourceFullName = "PATH/TO/NEW/FILE"
pptShape.LinkFormat.Update
End If
Next
Next
End Sub
This will work for the Paste Link case, but not the Link Data case, in which case pptShape.Type = msoChart. My question is if there is a way to make it work with Link Data as well. Wtih Paste Link, the SourceFullName property will point to a specific chart object, like filename1.xlsx!Chart 1, and changing it to filename2.xlsx!Chart 1 will work as expected. In contrast, under the Link Data option the SourceFullName property only points to filename1.xlsx and I cannot figure out how to see what chart object within the file it is pointing to. Regardless, if I change SourceFullName to filename2.xlsx no error will be thrown, but as far as I can tell the pointer is still to filename1.xlsx, as the chart doesn't change.
I have currently written an Excel VBA code which creates 40+ PowerPoint Slides.
However, while running this code PowerPoint crashes after creating 22 slides.
This issue only occurs when the VBA code is run at one go. Because, when I try to run the same code line by line it runs successfully till end.
For me this is something weird. Do we have any suggestion for this problem ?
=> My code till creation of slide 2 is listed below (thereafter it create the other slides one after another till 43rd Slide)
Regards,
Alok
Sub test25()
Dim pApp As PowerPoint.Application
Dim pPres As PowerPoint.Presentation
Dim pSlid As PowerPoint.Slide
Set pApp = New PowerPoint.Application
pApp.Visible = True
pApp.Activate
Set pPres = pApp.Presentations.Open("C:\.....\Template.pptx")
pPres.Slides(1).Select
Sheets("S01").Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pPres.Slides(1).Shapes.PasteSpecial DataType:=wdPasteText
pPres.Slides(2).Duplicate
pPres.Slides(2).Select
Sheets("S02").Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pPres.Slides(2).Shapes.PasteSpecial DataType:=wdPasteText
End Sub
I see multiple potential issues, some of which are just code improvements.
On the line:
pPres.Slides(2).Duplicate
You are referring to slide 2, but you have not yet created slide 2 (as this is the line that creates it). Change this to:
pPres.Slides(1).Duplicate
I don't see how your code is running, even line by line, without that...unless 'template.pptx' already has all of the slides (in which case, why are you duplicating? I assumed 'template.pptx only contained the first slide)
Initially I suspected a race condition, but typically VBA handles these well. To be sure, and just general good practice, you may want to use a variable to refer to the current slide, instead of just a number (you already have the variable declared)
Set pSlid = pPres.Slides(1)
Sheets("S01").Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pSlid.Shapes.PasteSpecial DataType:=wdPasteText
Set pSlid = pSlid.Duplicate
...
Also for your own convenience, you way want to look into using a loop, like:
Set pSlid = pPres.Slides(1)
For i = 1 to 43
Sheets("S" & Format(CStr(i), "00")).Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pSlid.Shapes.PasteSpecial DataType:=wdPasteText
Set pSlid = pSlid.Duplicate
Next
The strangest thing is happening with my macro in Excel. It works like a charm, but when it has to copy 2 charts and paste into my powerpoint presentation, suddenly, the Chart isn't exactly the same.
My code:
Set Wb = Workbooks.Open("Path\WbName.xlsx", ReadOnly:=True, UpdateLinks:=0)
It opens 5 more workbooks... And then it goes through a loop, to copy all the Charts
Dim Charts_Arr As Variant
Charts_Arr = Worksheets("Parameters").ListObjects("Parameters").DataBodyRange.Value
For i = LBound(Charts_Arr) To UBound(Charts_Arr)
SourcePath = Charts_Arr(i, 8)
SheetName = Charts_Arr(i, 4)
ShapeNr = Charts_Arr(i, 2)
SlideNr = Charts_Arr(i, 3)
Schaling = Charts_Arr(i, 6)
Set Source = Workbooks(SourcePath)
Set PPpres = oPPTApp.ActivePresentation
Set Sh = Source.Sheets(SheetName).Shapes(ShapeNr)
Sh.Copy
Set NewSh = PPpres.Slides(SlideNr).Shapes.PasteSpecial(ppPasteJPG)
With NewSh
.Top = Charts_Arr(i, 5)
.Left = Charts_Arr(i, 7)
.ScaleHeight Schaling, msoTrue
End With
Next i
This goes perfectly. But when I take a look at the ppt-file, 2 charts are not exactly the same.
(TIP: Excel is Chartarea, not a shape - didn't know this at first)
When copy the picture manually, I get the correct picture:
And what's more bizarre, I have 2 other Charts on another Sheet in the same workbook who doesn't cause any problems.
Could this be a problem with links, or the way I copy?
UPDATE
If I adjust the code as suggested below:
Source.Sheets(SheetName).ChartObjects(ShapeNr).Chart.CopyPicture
Set NewSh = PPpres.Slides(SlideNr).Shapes.Paste
With NewSh
.Top = Charts_Arr(i, 5)
.Left = Charts_Arr(i, 7)
.ScaleHeight Schaling, msoTrue
End With
I get this:
I'm doing something wrong with the Paste part of the code, I guess.
Tried other possibilities, always end up getting no images, or the one above.
FIXERSUPDATE
So I made/used a loophole. Couldn't find a way to paste the images directly into Powerpoint, So I pasted it into an excelsheet 'Temp' instead. And adjusted the Array, and that seemed to work. But I still would like to know how to do this directly in Powerpoint.
Thanks in advance for your insights!
I couldn't find a PasteSpecial Option that could fix the issue.
CopyasPicture works, but I can't seem to figure out how to paste it directly into ppt. So I used a workaround. I created a 'Temp' Sheet, where I could paste the Chart as Picture in the right format, afterwards I could program it to paste the shape into Ppt. Not the cleanest way to solve the issue, but it works.
I have a Power Point presentation which ~200 slides. Each slide have one chart, which data is updated monthly by a link to a master xlsx file. In order to not show empty values (future months) in the charts, I have to open the data editor (chart right click > Edit data...) of every chart and select the range until the current month.
I wrote a macro for it in Power Point:
Sub Refresh_slides()
For i = 1 To ActivePresentation.Slides.Count
Set ObjSlide = ActivePresentation.Slides(i)
On Error Resume Next
Set mychart = ObjSlide.Shapes("Chart 3").Chart
mychart.Select
mychart.ChartData.Activate
Set wb = mychart.ChartData.Workbook
Set ws = wb.Worksheets(1)
Application.Run "Refresh_slides_AUX.xlsm!atual_slide"
wb.Close True
Next
End Sub
Refresh_slides_AUX.xlsm is an auxiliary macro worksheet to select the correct range of each chart (necessary because Power Point VBA, as long as I know, don't have an option to do it):
Sub atual_slide()
Windows("Gráfico no Microsoft PowerPoint").Activate
ActiveSheet.ListObjects("Table1").Resize Range("$A$1:$I$23")
ActiveWindow.Close SaveChanges:=True
End Sub
It works fine, but even after the range is resized the chart label doesn't reflect this change in source data. How can I force the appearance of the chart label to update? Seems like something is missing between lines
ActiveSheet.ListObjects("Table1").Resize Range("$A$1:$I$23")
and
ActiveWindow.Close SaveChanges:=True
like a "refresh" or "reset", but I can't figure it out...
Any ideas?
PS. By "label" I mean a data table (don't know how they call it in english) which is a label option in Office. Eg. below:
My intention is to open an existing PowerPoint presentation along with an existing Excel workbook, and subsequently run a VBA macro from Excel which would update the corresponding values in PowerPoint.
For this I've identified the Shape name of the corresponding text boxes I want to update in PowerPoint by highlighting the specific textbox and used Format -> Align. Then I've created 3 columns in Excel with the values:
Slide index Shape name Value
1 Title 2 =CONCATENATE("REPORT ";YEAR(TODAY()))
1 Placeholder for date1 =TODAY()
I use the macro (which I unfortunately can't remember from which site I copied it):
Sub writedata()
Dim c As Object
Dim shapeslide
Dim shapename
Dim shapetext
Set ppapp = GetObject(, "Powerpoint.application")
Set pppres = ppapp.ActivePresentation
For Each c In Blad2.Range("a2:a" & Blad2.Range("a" & Rows.Count).End(xlUp).Row)
shapeslide = Blad2.Range("a" & c.Row)
shapename = Blad2.Range("b" & c.Row)
shapetext = Blad2.Range("c" & c.Row).Text
pppres.Slides(shapeslide).Shapes(shapename).TextEffect.Text = shapetext
Next
End Sub
My problem is that Slide 1 wont be updated at all in its corresponding Shape name. The only action which happens when I execute this macro is that, for some reason, Slide 3 has its font size modified to become size 35 instead of size 16. I can't understand why that is happening. The Shape name of the shape whose font size is altered is neither written into the Excel workbook, nor is it the same shape name as one of those two written in Excel.
Hopefully someone can shed some light into this.
Lets get your slides and shapes listed by excel to ensure that they are what you expect. Sometimes they are really oddly named/IDed. Since you have slides not changing that should and slides changing that should not... we definitely need to doublecheck these. This will itterate through each slide and each shape on that slide and list the slide ID and Name and each shape ID and Name. I have a presentation and the first slide is slide 297 for some reason. Then slide 250 is second. Slide 50 is 3rd. The rest are all numbered oddly also. o.O
Turn on your immediates window to see the debug text.
Sub SlidesShapes()
Dim i As Integer, j As Integer
Set ppapp = GetObject(, "PowerPoint.Application")
Set ppres = ppapp.ActivePresentation
For i = 1 To ppres.Slides.Count'slides and shapes start counting at 1 not 0
Debug.Print ppres.Slides(i).SlideID
Debug.Print ppres.Slides(i).Name
For j = 1 To ppres.Slides(i).Shapes.Count
Debug.Print ppres.Slides(i).Shapes(j).ID
Debug.Print ppres.Slides(i).Shapes(j).Name
Next
Next
End Sub
Also, when you step through your original code (not this snippet) what do you see in your locals window for each step? Anything weird going on there that jumps out at you? Any variables populated with something unexpected or not completely right?