Activate the corresponding ribbon tool tab - vba

Programmatically select a shape on PowerPoint slides doesn't activate the corresponding ribbon tool tab.
The attached image shows that when selecting an "Audio Shape" manually, the ribbon tab of "Audio Tools" is then automatically visible. But when selecting the shape through a VBA code e.g. oShp.Select, the ribbon tab of "Audio Tools" will not visible.
Is there any special trick to make the corresponding ribbon tool tab visible when selecting a shape programmatically?
I need to access some commands through the SendKeys method as I can't/ don't know how to set some properties of the audio shape (e.g. audio fade-out duration, volume, starts).
EDIT
Further testing.
The following code will make the ribbon tab visible
Sub test()
Dim pres As Presentation
Dim sld As Slide
Dim shp As Shape
Set pres = ActivePresentation
Set sld = pres.Slides(1)
Set shp = sld.Shapes(1)
shp.Select
End Sub
However, if I add more lines after the select statement, the ribbon tab will not visible, e.g.
Sub test()
Dim pres As Presentation
Dim sld As Slide
Dim shp As Shape
Set pres = ActivePresentation
Set sld = pres.Slides(1)
Set shp = sld.Shapes(1)
shp.Select
DoEvents
SendKeys "%jn%v%{DOWN}", True 'Volume=low
'SendKeys "%jn%u%{UP}", True ' Start: play across slides
SendKeys "%jn%o5~", True 'Fade out: 5s
End Sub

You shouldn't need to use SendKeys.
The code below will change some of the audio options.
Sub Test()
Dim sld As Slide
Dim shp As Shape
Set sld = ActivePresentation.Slides(1)
Set shp = sld.Shapes("Recorded Sound")
With shp.MediaFormat
.FadeInDuration = 1000
.FadeOutDuration = 1000
.StartPoint = 0
.EndPoint = 4000
.Volume = 0.8
End With
End Sub
I'll admit I haven't found how to Play Across Slides or Rewind after Playing.

Related

How to bypass Excel popups when using VBA in PowerPoint to update msoLinkedOLEObject

I have a PowerPoint with links from multiple Excel spreadsheets. I would like to update the linked object with a macro.
The macro below will generate 2 types of popup. Popup will appear for each link to be updated in my case about 30 times. Clicking cancel will allow macro to continue.
1) Microsoft Excel has stopped working (close program)
2) File in use (Read Only, Notify, or Cancel options)
Is there a way to bypass these messages?
Sub linkupdate()
Dim osld As Slide
Dim oshp As Shape
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.Type = msoLinkedOLEObject Then
If LCase(oshp.LinkFormat.SourceFullName) Like "*defect 95R*" Then
oshp.LinkFormat.AutoUpdate = ppUpdateOptionManual
oshp.LinkFormat.Update
oshp.LinkFormat.AutoUpdate = ppUpdateOptionAutomatic
End If
End If
Next
Next
MsgBox "Finished updating Charts", , "Update Complete"
End Sub
This code prevented the following alerts from occurring
1) Microsoft Excel has stopped working (close program)
2) File in use (Read Only, Notify, or Cancel options)
After completion macro popup it can take a minute before the user gains control of PowerPoint. I'm assuming excel alerts are being closed in the background as there are over 30 link charts.
I'm a newbie at VBA so this code may not be efficient.
Sub linkUpdate()
Const xFile = "C:\temp\defect 95R.xlsx"
Dim pptPresentation As Presentation
Dim osld As Slide
Dim oshp As PowerPoint.Shape
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Workbooks.Open xFile, ReadOnly:=True, Notify:=False
xlApp.Workbooks.Application.DisplayAlerts = False
Set pptPresentation = ActivePresentation
'Loop through each slide in the presentation
For Each osld In pptPresentation.Slides
'Loop through each shape in each slide
For Each oshp In osld.Shapes
'Find out if the shape is a msoLinkedOLEObject type=10
If oshp.Type = msoLinkedOLEObject Then
'Only update shape if file name contains defect 95r
If LCase(oshp.LinkFormat.SourceFullName) Like "*defect 95r*" Then
oshp.LinkFormat.AutoUpdate = ppUpdateOptionManual
xlApp.Workbooks.Application.DisplayAlerts = False
oshp.LinkFormat.Update
oshp.LinkFormat.AutoUpdate = ppUpdateOptionAutomatic
End If
End If
Next
Next
xlApp.Workbooks.Close
xlApp.Workbooks.Application.Quit
Set xlApp = Nothing
MsgBox "Finished updating Charts", , "Update Complete"
End Sub

VBA code that will find a named chart in a Power Point presentation

I have been able to, with support from site members, create a user form in a Power Point slide to update an embedded chart. In order to fully utilize the user form, VB code must used to loop through all slides because the named chart may move between slides. The name of the chart is "DVPVchart". The code being used continues to get an error at the first Set assignment. Code is below. Tried multiple arrangements and variants of the loop but continue to return the same error. Any guidance is appreciated.
Private Sub AddDVSetUp_Click()
Dim sld As slide
Dim shp As shape
Dim chrt As Chart
Dim xlWB As Object
For Each sld In ActivePresentation.Slides
Set shp = sld.Shapes("DVPVchart")
Set xlWB = shp.Chart.ChartData.Workbook
'Find first sheet of embedded Chart In PowerPoint
With xlWB.Sheets(1)
'location in Chart In PowerPoint = UserForm Textbox
.Range("C4").Value = Gate2Date.Value
.Range("C11").Value = OldestSurrogateDate.Value
End With
Next sld
End Sub
It returns error since you didn't put a check if the chart exist in the said slide. You need to add a check if the chart matches your criteria. Try:
For Each sld In ActivePresentation.Slides
'/* You will need another loop to check each shape */
For Each shp In sld.Shapes
If shp.Type = msoChart Then '/* check for specific type of shape */
If shp.Name = "DVPVchart" Then '/* chech chart for specific name */
Set xlWB = shp.Chart.ChartData.Workbook '/* assign it */
Exit For '/* exit since you got what you need */
End If
End If
Next
If Not xlWB Is Nothing Then Exit For '/* exit if you already set your xlWB object
Next
'/* Rest of your code go here */

VBA paste msoChart into Powerpoint 2010

I am trying to paste an msoChart object with embedded data from the clipboard into PowerPoint 2010 using VBA. (chart created in Excel 2010).
The only examples that I can find involve either linking the Chart to an Excel file or creating a msoEmbeddedOLEObject.
If I manually paste in PowerPoint 2010 I get a paste option to "Embed Workbook". However it is not available within manual "Paste Special".
So it would seem that something in addition to pasting the chart is needed. But I am unsure what that is or how to go about it.
What I have tried is
Sub PasteExample()
Dim Sld As Slide
Dim Shp As ShapeRange
Set Sld = ActiveWindow.View.Slide
'# This pastes clipboard content as a linked chart
Set Shp = Sld.Shapes.Paste
End Sub
Sub PasteExample2()
Dim Sld As Slide
Dim Shp As ShapeRange
Set Sld = ActiveWindow.View.Slide
'# This option does not work, object is still linked
'Set Shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteDefault, Link:=msoFalse)
'# This option does not work, object is still linked
'Set Shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteShape, Link:=msoFalse)
'# I'm not after OLEObjects
'Set Shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteOLEObject, Link:=msoFalse)
End Sub
Many thanks if you can shed some light.
We don't see what you are copying and how, plz join code if problem not solved
Here are the members of PpPasteDataType that you can use with PasteSpecial in PowerPoint :
Usually, I use that code as a base, it should help you :
Sub Export_to_Ppt()
'
Dim Ppt As PowerPoint.Application, _
Pres As PowerPoint.Presentation
Set Ppt = CreateObject("PowerPoint.Application")
Set Pres = Ppt.Presentations.Open("I:\Template DTC.potx")
Ppt.Visible = True
Sheets("Graph1").ActiveChart.ChartArea.Copy
Pres.Slides.Add Index:=Pres.Slides.Count + 1, Layout:=ppLayoutTitleOnly
'Pres.Slides(Pres.Slides.Count).Shapes.Paste
Pres.Slides(Pres.Slides.Count).Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile, Link:=False
Pres.Slides(Pres.Slides.Count).Shapes.Title.TextFrame.TextRange.Text = "Chart Title"
Pres.SaveAs _
Filename:="I:\TestNaz.ppt", _
FileFormat:=ppSaveAsOpenXMLPresentation
Set Ppt = Nothing
Set Pres = Nothing
End Sub
I tried to reproduce your example with PowerPoint 2013. I wasn't able to reproduce the behaviour that you describe.
Pre-Condition: I copied an Excel 2013 chart to the Clipboard (just the chart, not the whole worksheet or anything else).
Invoking either Sld.Shapes.Paste or Sld.Shapes.PasteSpecial(DataType:=ppPasteDefault,Link:=msoFalse) will insert an msoChart into Powerpoint:
Set shp = Sld.Shapes.Paste
MsgBox shp.Type ' returns 3 that is msoChart
Set shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteDefault, Link:=msoFalse)
MsgBox shp.Type 'returns 3 that is msoChart
Those charts are properly formatted in the current PowerPoint style and I can right-click them to edit the data.
Especially, they are embedded, not linked.
For comparison I also tried:
Set shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteOLEObject, Link:=msoFalse)
MsgBox shp.Type ' returns 7 that is msoEmbeddedOLEObject
Set shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteOLEObject, Link:=msoTrue)
MsgBox shp.Type ' returns 10 that is msoLinkedOLEObject
Set shp = Sld.Shapes.PasteSpecial(DataType:=ppPasteDefault, Link:=msoTrue)
MsgBox shp.Type 'returns 10 that is msoLinkedOLEObject
When I right-click those in Powerpoint, then there is are menu entries to manipulate the “Worksheed Object” respectively the “Linked Worksheed Object”.
So either I misunderstand what you mean by "linked", or there is a bug in PP 2010, or you are having something different in your clipboard.
I did come upon a solution on another form.
Once the chart is in the clipboard.
Execute the following line in PowerPoint 2010
Application.CommandBars.ExecuteMso "PasteExcelChartDestinationTheme"
It gave me just what I was after.

PowerPoint VBA - loop all slides, all shapes, find chart, set datalabel color to Black

I'm new to PowerPoint VBA so please bear with me.
I would like to:
loop through all the slides on a given pptx,
loop through all the shapes on a given slide,
find chart shapes
loop through the chart's series collection
set the datalabel color property to dark black.
So far I have been able to complete the first 3 tasks but I need help with the last 2. Here is my code:
Sub test()
Dim slide As Object
Dim shape As Object
Dim shapeNames As Object
Dim chSeries As Series
i = 0
For Each slide In ActivePresentation.Slides
For Each shape In slide.Shapes
If shape.HasChart Then
i = i + 1
Debug.Print "found a chart on slide", i
End If
Next
Next
End Sub
Solved.
Sub test()
Dim sld As Slide
Dim shp As Shape
Dim sr As Series
Dim chrt As Chart
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Debug.Print shp.Chart.ChartType
If shp.Chart.ChartType = 57 Then
shp.Chart.SeriesCollection(1).DataLabels.Font.Color = RGB(0, 0, 0)
End If
End If
Next shp
Next sld
End Sub
Though I didn't successfully loop over the series in chart but this works.

Editing embedded objects in powerpoint

I have a powerpoint presentation with an excel workbook embedded in one of the slides. I also have a userform that I want the user to input information into, I want to take this information and then edit the excel sheet with the relevant information.
I don't know how to access the excel sheet within powerpoint though so I can change the values of the cells.
Sub a()
Dim oSl As PowerPoint.Slide
Dim oSh As PowerPoint.Shape
Set oSl = ActivePresentation.Slides(1)
Set oSh = oSl.Shapes(1)
With oSh.OLEFormat.Object.Sheets(1)
.Range("A1").Value = .Range("A1").Value + 1
.Range("A2").Value = .Range("A2").Value - 1
End With
Set oSl = Nothing
Set oSh = Nothing
End Sub
Inspired in this code