Add Watermark to PowerPoint Slide from Excel - vba

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

Related

What are some other methods I can use INSTEAD of "ActivePresentation" in PowerPoint VBA

I'm currently working on PowerPoint VBA and writing a code to be able to copy the first slide and paste it again in the same presentation. However, I keep getting the error "Clipboard is empty or contains data which may not be posted here" and according to Microsoft page the problem is the use of "ActivePresentation"
I'm looking for another way to refer to the slide that I have open without using ActivePresentation. Any help? Ideas?
The line of code I use ActivePresentation is below:
ActivePresentation.Slides(1).Copy
ActivePresentation.Slides.Paste(ActivePresentation.Slides.Count=1)
Don't copy, duplicate
Dim NewSlide as Slide
Set newSlide = ActivePresentation.Slides(1).Duplicate
NewSlide.MoveTo toPos:=4 'move it to become the fourth slide
Slight variant on Harassed Dad's solution. PPT barks at the Set line because Duplicate returns a SlideRange object rather than a Slide object. .Duplicate(1) returns the first slide in the range as a Slide object. This duplicates slide 2 and moves it to the first position in the presentation.
Sub CopySlide()
Dim oSl As Slide
With ActivePresentation
Set oSl = .Slides(2).Duplicate(1)
oSl.MoveTo (1)
End With
End Sub

Referencing Charts By Name Only in PowerPoint VBA

I have been searching for hours to try to find the answer to this question, but to no avail, so I'm hoping I can find the answer here.
I want to create a variable that refers to a pre-existing chart in PowerPoint so I can start automating its data. I want to refer to the chart by its name to make things very easy, but no matter what I do I cannot seem to give PPT a satisfactory Chart address.
I have tried almost every possible variation of the below, but without success:
Dim chrtPP As PowerPoint.Chart
Set chrtPP = ActivePresentation.Slides(1).Shapes.Charts("Chart3")
Could someone please tell me what I'm doing wrong?
Thanks!
You need to reference the shape by name (a 'Shape" in PowerPoint is actually any object that is on a slide and can be a simple shape, textbox, table, chart, group, media clip etc.). If you're on PowerPoint 2010 and higher, press Alt+F10 to open the selection pane to find the name of the selected chart object. It may be a standard chart object or a chart within a placeholder object. You can then reference the chart as follows:
Option Explicit
Sub ChartStuff()
Dim oShp As Shape
Dim oCht As Chart
Set oShp = ActivePresentation.Slides(1).Shapes("Chart 3")
If oShp.HasChart Then
Set oCht = oShp.Chart
End If
' Do stuff with your chart
If oCht.HasTitle Then Debug.Print oCht.ChartTitle.Text
' Clean up
Set oShp = Nothing
Set oCht = Nothing
End Sub
The key in programming PowerPoint is to ignore the object name in the Object Model for 'Shape' as it's very misleading!

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

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

How do I paste a Shape into the notes page of a Powerpoint presentation in VBA?

I am trying to use the following code to paste a copied shape into a note page, but the Paste commands fails with the error "The specified data type is unavailable". It works if the clipboard contains texts instead of a shape. Any help is appreciated
Dim oSlide As Slide
Dim oShapes As Shapes
Set oSlide = ActivePresentation.Slides.Item(1).NotesPage.Item(1)
Set oShapes = oSlide.Shapes
oShapes.Paste
This was a known bug in PPT 2007/2010 but is fixed in 2013.
For 2007/2010 switch to the notes page view, move to the desired notes page and then use ActiveWindow.View.Paste to get the same effect.

How to switch between "active paper" and "slides" in Power Point VBA

I have a simple question regarding PowerPoint VBA:
Which VBA code should I use to switch between the "active sheet of paper" (I am sorry I don't know how to name it properly), in which I am doing something with the object(s), and the file (or "field", again sorry for my poor terminology) where all the slides are ?
For example, if I want to move a selected object in the "active sheet of paper" I would use this macro:
ActiveWindow.Selection.ShapeRange.IncrementLeft
6#
and if I want to copy the selected slide in the slides file, I would use this code:
ActiveWindow.Selection.Copy
ActiveWindow.View.Paste
But how can I connect these two pieces of script? Let's say I want to move an object in the "active sheet of paper", then copy this whole "sheet", then create its twin in the slides field, and then jump into the twin sheet of paper to do something with objects there?
Shortly, how do I switch from "paper" to "slides" and back to "paper" in VBA?
(Again, I am sorry for terrible terminology here, I hope you understand what I mean here.)
Thank you all in advance.
If you record a macro in PowerPoint and examine the code, you'll see that it uses the Selection object for just about everything. That's sometimes useful (because it means it's more likely that the code will do what you want if you select another object), but for anything more than a very short macro, it's probably better to refer to the objects directly, as in the following code:
Sub Test()
' Get the active presentation
Dim oPresentation As Presentation
Set oPresentation = ActivePresentation
' Get the first slide in the presentation
Dim oSlide As Slide
Set oSlide = oPresentation.Slides(1)
' Get the first shape on the slide
Dim oShape As Shape
Set oShape = oSlide.Shapes(1)
' Nudge the shape to the right
oShape.Left = oShape.Left + 1
' Copy the whole slide
oSlide.Copy
' Paste the slide as a new slide at position 2
Dim oNewSlides As SlideRange
Set oNewSlides = oPresentation.Slides.Paste(2)
' Get a reference to the slide we pasted
Dim oNewSlide As Slide
Set oNewSlide = oNewSlides(1)
' Get the first shape on the NEW slide
Dim oNewShape As Shape
Set oNewShape = oNewSlide.Shapes(1)
' Nudge the shape to the right
oNewShape.Left = oNewShape.Left + 1
End Sub
Note that pretty much every object has a Select method, so if you do want to explicitly select something, you can. In some cases, you may need to change the active window's view type first - so for example, you can't select a shape on a slide while in slide-sorter view.