Creating a button in PowerPoint VBA, but it's not clickable - vba

I'm working in Excel VBA, creating a PowerPoint presentation.I am trying to place Next and Previous buttons on each slide. I am using the code below:
Dim ppApp As PowerPoint.Application, ppPres As PowerPoint.Presentation, ppSlide As PowerPoint.Slide
Dim shpNextButton As PowerPoint.Shape
Set ppApp = CreateObject("Powerpoint.Application")
Set ppPres = ppApp.Presentations.Open("C:\Users\test1.pptm")
Set ppSlide = ppPres.Slides.Add(ppPres.slides.count + 1, ppLayoutBlank)
Set shpNextButton = ppSlide.Shapes.AddShape(msoShapeActionButtonForwardorNext, 750, 480, 40, 12.5)
With shpNextButton.TextFrame.TextRange
.Text = "Next"
With .Font
.Size = 10
.name = "Arial"
End With
End With
shpNextButton.ActionSettings(ppMouseClick).Action = ppActionNextSlide
This code creates the button with the correct text on it. However in the PowerPoint slide, the button is clickable. When I click on it, it just acts like a regular shape.

Your code works for me when I use an adapted version of it (removing lines 1,3,4 and setting a reference to the current slide for line 5) within the PowerPoint VBE so the only thing I can think of is that the 'pp' constants are not set if you haven't added the PowerPoint library to your project. What do you get in the Immediate window if you type this?
?ppActionNextSlide
It should return 1 if all is ok. If it returns 0 then that's equivalent to ppActionNone which would explain what's happening.
By the way, your code added the button outside of my 4:3 slide so I assume you're using a 16:9 slide layout. It would be better to reference the right hand side of the side less an offset to avoid this potential issue with:
ActivePresentation.PageSetup.SlideWidth

Related

PowerPoint Add-in Add a textbox to a selected slide

I'm trying to make a button that when clicked, will add two text boxes to the selected slide in a specified place with specified formatting (font, size, color, justified). I've been trying to reverse engineer anything applicable, but just end up breaking things. This set of code will allow me to make a rectangle (not a textbox which is preferred) size it and place it (just 1 not 2) with sample text.. For the life of me I cant figure out how to make it create a textbox on the selected slide or active window.. what am I doing wrong? Heres the code I found..
Sub AddTextBox()
Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes _
.AddTextBox(msoShapeRectangle, 180, 175, 350, 140).TextFrame
.TextRange.Text = "Ctrl+A(Select all), Ctrl+V(Paste)"
.MarginTop = 10
End With
End Sub
To refer to the slide that's currently being displayed, you can use the Slide Property of the View object...
Dim mySlide As Slide
Set mySlide = ActiveWindow.View.Slide
For creating a textbox, here's the proper syntax as per the documentation here...
Syntax
expression. AddTextbox( Orientation, Left, Top, Width,
Height )
expression A variable that represents a Shapes object.
So, in your case, it would be something like this...
Dim myTextbox As Shape
Set myTextbox = mySlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 180, 175, 350, 140)
Change the text orientation as desired.

Add Watermark to PowerPoint Slide from Excel

I am trying to add a watermark to a PowerPoint slide from Excel with VBA and don't know where to start. I have searched on Google and found nothing. There is one question on Stackoverflow that helped a little but I couldn't follow it. I am wondering if someone could refer me to somewhere or point me in the right direction? Again, I just want to add a watermark to one of the slides in Master View. Thanks!
To change a slide in Master View, you can work with the CustomLayouts collection.
Note that you'll have to refer to a specific CustomLayout by its index, and not its Name, as this question points out.
This example code
creates or gets a PowerPoint instance
creates a new Presentation
copies Picture 1 and pastes it in the Shapes collection of the first CustomLayout, which for me is the Title Slide Layout.
I assume from here you can modify its size/position or make any other desired changes.
Sub AddWatermark()
Dim wmark As Shape: Set wmark = ThisWorkbook.Sheets("Sheet1").Shapes("Picture 1")
Dim PPT As PowerPoint.Application
Dim pres As PowerPoint.Presentation
On Error Resume Next
Set PPT = GetObject(, "PowerPoint.Application")
On Error GoTo 0
If PPT Is Nothing Then
Set PPT = New PowerPoint.Application
End If
PPT.Visible = True
Set pres = PPT.Presentations.Add
wmark.Copy
pres.SlideMaster.CustomLayouts(1).Shapes.Paste
End Sub
My Original Watermark
Title Slide Layout showing applied watermark

.Left positioning shapes in wrong place in PowerPoint 2016 VBA

I'm trying to copy ranges from an Excel sheet and paste them into slides as tables, then position and resize them. However, when I try to position the shapes in ppt, the .Left method doesn't behave as expected; in the slide thumbnails on the left of the screen the shapes are in the expected positions (centred as in the below code), but when I select the slide the shapes are shifted far to the right of where they ought to be.
I thought it may be using 'centre' as its reference point instead of the left bound of the slide but this doesn't match up with the amount it's offset.
Would really appreciate some help with this - would be good to know if it's a bug in ppt or an error in my code as if it's a bug it has a large impact on the feasibility of this project. For this particular example I could probably use the .Align method as a workaround but for later slides I need to be able to position multiple shapes on the same slide accurately.
I'm using Office 365.
Code below:
Sub PP_export()
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim XLws As Worksheet
Set PPApp = New PowerPoint.Application
Set XLws = ActiveSheet
Set PPPres = PPApp.Presentations.Open("Y:\Research\PROJECTS\2018\Magic Macro\ppt_template_.potx")
PPApp.Visible = True
''Lifestyle Statements
'By Col%
Set PPSlide = PPPres.Slides(3)
Dim LSCol As PowerPoint.Shape
XLws.Range("M106:o126").Copy
PPSlide.Shapes.PasteSpecial ppPasteDefault
Set LSCol = PPSlide.Shapes("Table 2")
With LSCol
.Left = (28.35 * 10.56)
.Top = (28.35 * 3.83)
.Height = (28.35 * 13.21)
.Width = (28.35 * 12.75)
End With
'By Index
Set PPSlide = PPPres.Slides(4)
Dim LSIndex As PowerPoint.Shape
XLws.Range("Q106:s126").Copy
PPSlide.Shapes.PasteSpecial ppPasteDefault
Set LSIndex = PPSlide.Shapes("Table 2")
With LSIndex
.Left = (28.35 * 10.56)
.Top = (28.35 * 3.83)
.Height = (28.35 * 13.21)
.Width = (28.35 * 12.75)
End With
I got the same problem and I run a debug message showing the position after inserting and it seems to be correct. But only the thumbnail looks fine, the actual slide is wrong.
I found out that the position of the table will be correct when activating or displaying the slide before pasting the table. Also if several tables need to be inserted I needed to make a short break of 2 seconds between the paste operations.
My workaround is pretty ugly and I am still searching for the reason. It is likely a bug but maybe it also has to do with some kind of offset of the slide in the background.
You need to loop over the table and the data if you are using a template.
If you already have a table (table 2) on slide 3, then you could use this. just send in the excel file object and the template (path+name)
it may need some tinkering.
Sub Slide_3(ByRef xlWorkBook, Template)
For Each oSh In Presentations(Template).Slides(3).Shapes
Select Case oSh.Name
Case "Table 2"
For i = 1 To 20
For j = 1 To 3
oSh.Table.Cell(i, j).Shape.TextFrame.TextRange.Text = xlWorkBook.Worksheets(24).Cells(105 + i, j + 12).Value
Next j
Next i
'M106:o126
End Select
Next oSh
End Sub

Change the text color of the chart title in a PowerPoint Histogram chart

I am trying to change the text color of the chart title of a histogram chart in PowerPoint.
Here is what I do:
var colorFormat = chart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor;
colorFormat.RGB = ...;
// or
colorFormat.ObjectThemeColor = ...;
This works for the standard charts like line charts. But it doesn't work for other chart types like histogram, waterfall, tree map etc.
In these cases, setting ObjectThemeColor sets the text to black. Setting RGB does actually set the correct color. However, in both cases, as soon as the user changes the selection, the text color jumps back to the one it had previously.
How can I set the text color of the title of one of these charts?
I am using VSTO and C# but a VBA solution is just as welcome as long as it can be translated to C# and still work.
Based on what info you gave I built a histogram and waterfall chart in PowerPoint and was successful using:
Sub ChartTitleFontColor()
Dim oShp As Shape
Dim oCht As Chart
'Waterfall on slide 1
Set oShp = ActivePresentation.Slides(1).Shapes(1)
If oShp.HasChart Then
Set oCht = oShp.Chart
End If
' Do stuff with your chart
If oCht.HasTitle Then
Debug.Print oCht.ChartTitle.Text
oCht.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(251, 5, 40)
End If
'Histogram on slide 2
Set oShp = ActivePresentation.Slides(2).Shapes(1)
If oShp.HasChart Then
Set oCht = oShp.Chart
End If
' Do stuff with your chart
If oCht.HasTitle Then Debug.Print oCht.ChartTitle.Text
oCht.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(251, 5, 40)
End If
' Clean up
Set oShp = Nothing
Set oCht = Nothing
End Sub
Your code works in my test. I created two charts in PowerPoint 2016, the first one a waterfall, and the second another type. The following code changes the title color only (and text just a proof of it being changed) and nothing else. I can select the other chart and nothing changes. I could not find a bug about this in a search. Perhaps something in the remaining code is changing it back?
Sub test()
Dim myPresentation As Presentation
Set myPresentation = ActivePresentation
Dim myShape As Shape
Set myShape = myPresentation.Slides(1).Shapes(1)
Dim theChart As Chart
If myShape.HasChart Then
Set theChart = myShape.Chart
If theChart.ChartTitle.Text = "This is blue" Then
theChart.ChartTitle.Text = "This is yellow"
theChart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 255, 0)
Else
theChart.ChartTitle.Text = "This is blue"
theChart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 255, 255)
End If
End If
End Sub
This is not exactly an answer but I think you should name your object.
Instead of using
ActivePresentation.Slides(1).Shapes(1)
You can name the object.

Code stopped working in Powerpoint 2016

Code that perfectly works in earlier versions of PPT stopped working in 2016.
When I try to change the left property of a shape in a chart, I get a Method left of object shape failed error.
I can perfectly read the .Left property.
I am running out of ideas? What can I do?
Sub test11()
Dim sld As Slide
Dim objChart As Object
Dim shpBubble As Object
Set sld = ActivePresentation.Slides("ScatterPlot01_Purch6")
Set objChart = sld.Shapes("Chart01").Chart
sld.Select
objChart.Select
Set shpBubble = objChart.Shapes("P01")
'shpBubble.Select
Debug.Print shpBubble.Left, shpBubble.Visible
shpBubble.Left = 10
End Sub
UPDATE
Having tested in PowerPoint 2010 and 2013, where it works, this now looks like a bug in 2016!
* END *
I managed to recreate the error in PowerPoint 2016 (PC) by manually adding a shape to a test chart (select the chart then click Format / Insert Shapes) and trying to write to several of it's properties including position and formatting such as changing fill colour. All generate an error.
Maybe one workaround is to use the .Delete method to delete the desired shape and then add a new shape at the required size and position. Something like this:
Sub test11()
Dim sld As Slide
Dim objChart As Chart 'Object
Dim shpBubble As Shape 'Object
Set sld = ActivePresentation.Slides("ScatterPlot01_Purch6")
Set objChart = sld.Shapes("Chart01").Chart
sld.Select
objChart.Select ' this won't work as you can only select the parent shape sld.Shapes("Chart01")
With objChart
.Shapes("P01").Delete
.Shapes.AddShape msoShapeOval, 10, 10, 20, 20
End With
End Sub
The challenge is that because the new shape is added as read only, the formatting can't be set!