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
Related
I haven't been able to find any workable solution for this in a couple hours of searching, so here I go.
I am exporting some data from access to excel via vba, formatting the excel and moving worksheets around, and then generating a chart. When I was testing it all in an excel macro I was able to use the SetDataSource method to adjust the range, but when I moved over to Access it didn't like this method, and will throw a run-time error of 438, "Object doesn't support this property or method". Simplified code below.
Dim xl as Object, wb as Object
Set xl = CreateObject("Excel.Application")
With xl
.Visible = False
.displayalerts = False
Set wb = .workbooks.Open(wbookpath)
'formatting code that all works fine
.Charts.Add
'add was called from sheet 11, popped up before 11 and am moving to end
.Sheets(11).move after:=.Sheets(12)
With Charts(1)
.SetSourceData .Range("Master!$A$1:$G$11")
'other chart formatting code that all works fine
End With
.activeworkbook.Close (True)
.Quit
End With
I have also tried changing the source, e.g.
.SetSourceData .Sheets("Master").Range("$A$1:$G$11")
changing where I call it from, e.g.
.Charts.Add
wb.Charts(1).SetSourceData .Range("Master!$A$1:$G$11")
With .Charts(1)
'rest of code
but it doesn't affect the error being thrown.
How can I get Access to adjust the source of my chart? If I can get this working then I would be able to go forward with making a slew of other charts as well.
This syntax worked for me:
With wb.Charts(1)
.SetSourceData Source:=Sheets("Master").Range("$A$1:$G$11")
'other chart formatting code that all works fine
End With
Reference https://learn.microsoft.com/en-us/office/vba/api/excel.chart.setsourcedata
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
I'm building a chart automation script in powerpoint and i have any issue when calling upon "ActivateChartDataWindow".
I would use "Activate" instead of "ActivateChartDataWindow", but "Activate" loads the full Excel program and makes the whole routine run slow and ulgy.
The problem I have is that "ActivateChartDataWindow" does work to populate the charts, but when I manually go to edit the data - right click, edit data - to access the excel application, it does not seem to want to load!
It has been driving my crazy for the last 5 hours and would appreciate any ideas on how to over come this.
OLE.dlll are working correctly and the code I am using is given below.
Code below:
There are 5 slides with one chart on each page and the code below is what i am using as a point of concept
I have a felling i am using "ActivateChartDataWindow" wrong, but there is not much on the web to know what i am doing wrong! Arrrhhhh!
For i = 1 To 5
Set instance = Nothing
Set instance = ActivePresentation.Slides(i).Shapes(1).Chart.ChartData
With instance
.ActivateChartDataWindow
instance.Workbook.Sheets(1).Range("A1:H26").Value = 27
instance.Workbook.Close
End With
Next i
End Sub
As always recommended, you don't need to Activate an object to modify it. If you're trying to handle a Workbook embedded in a slide, you can do it this way
' This function will get you a Workbook object embedded in a Slide (late binding)
Function getEmbeddedWorkbook(sld As Slide) As Object
Dim shp As Shape
On Error Resume Next
For Each shp In sld.Shapes
If shp.Type = 3 Then ' embedded chart workbook created in PP
Set getEmbeddedWorkbook = shp.Chart.ChartData.Workbook
Exit Function
End If
If shp.Type = 7 Then ' embedded workbook pasted from excel
Set getEmbeddedWorkbook = shp.OLEFormat.Object
Exit Function
End If
Next
End Function
' For Testing, I have 6 slides, Some have a workbook pasted from Excel
' OLE, shape type = 7, others have a chart created in PP (type = 3)
Sub Test()
Dim wb As Object, i As Long
For i = 6 To 6 'ActivePresentation.Slides.Count
Set wb = getEmbeddedWorkbook(ActivePresentation.Slides(i))
If Not wb Is Nothing Then
wb.Sheets(1).Range("A1:D5").Value = i * i
End If
Next
End Sub
I created code to set the pagebreaks in an excel report to deal with the orphan issue (i.e. one line of text spills over onto the next page, etc.). The code works fine when I run it with the report open / visible.
It is part of a larger application which is opened and the code executed from MS Access. Excel is not visible during the process to improve performance.
When I run my code from MS Access it no longer works... it doesn't produce an error, but simply ignores the actual pagebreak setting command.
I read on various forums that in order to avoid this problem, excel needs to be first switched over to ActiveWindow.View = xlPageBreakPreview, but that doesn't work either (I suspect since Excel isn't visible).
I have tested for the following:
Code works when it is started manually or stepped through with F8
Code is executed when called upon from Access (I set breakpoints)
Switching the window view doesn't do anything either
How can I get Excel to change the pagebreaks via code when Excel is run in the background?
This is my code:
Sub TheOrphanProblem()
Dim iPageBrkRow
'Determine if there are page breaks and if so on which row of the document
If FindNthAutoPageBreak(wsRptHolding, 1) Is Nothing Then
'No pagebreak found so we exit the sub
Exit Sub
Else
iPageBrkRow = FindNthAutoPageBreak(wsRptHolding, 1).Row 'Get row
End If
Debug.Print iPageBrkRow
Dim x As Integer
Dim sCase As String
Dim rNewposition As Range
With wsRptHolding
'Code edited out for brevity. This part checks if there is an orphan problem and finds the new position for a pagebreak if needed.
It then provides that position as a range called "rNewposition".
'Moves page break to calculated position
ActiveWindow.View = xlPageBreakPreview
.HPageBreaks.Add rNewposition
ActiveWindow.View = xlNormalView
End With
End Sub
This is the code I use to find the pagebreak positions...
Private Function FindNthAutoPageBreak(Sht As Worksheet, Nth As Long) As Range
'Set page break of the last page so that sub asset groups are kept together
Dim HP As HPageBreak
Dim Ctr As Long
For Each HP In Sht.HPageBreaks
If HP.Type = xlPageBreakAutomatic Then
Ctr = Ctr + 1
If Ctr = Nth Then
Set FindNthAutoPageBreak = HP.Location
End If
End If
Next HP
End Function
Try this
ActiveSheet.DisplayPageBreaks = True
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: