In the Powerpoint VBA editor, we get a list of slides in the VBA project window, categorised under "Microsoft PowerPoint Objects". This list will include slides that have ActiveX controls on them.
The slides appear with numbers on them ("Slide1", "Slide3", etc), and these numbers look like they are based on the order in which the slides were added to the presentation - not the actual order of the slides in the current presentation. However, the title or name of the slides is not included. This makes it confusing to work with, and difficult to find the slide that has the control one wants to work with.
In Excel VBA, the layout of the editor is the same, with a list of worksheets. However, in Excel the name of the sheet is shown in brackets after the sheet number. So if Sheet1 is called "MyDataSheet", it will show up as "Sheet1 (MyDataSheet)".
How can I achieve something similar in Powerpoint? Is there are way to control the name/title that is used to display each slide in the Powerpoint editor?
This is one of the oddities of the PPT object model. Each slide has an internal name that's assigned by PPT at the time the slide is created. That's what you see in the IDE. Each slide also has a .Name property that's initially the same as the slide name assigned by PPT, but that you can change programmatically.
PPT still shows the assigned name under "Microsoft PowerPoint Objects" but if you look at the Properties window of the IDE (press F4 to display it) you'll see (and can edit) the slide's .Name property. This would let you know which slide you've selected in the IDE.
You could also run a bit of code to change the .Name property in a way that reflect's the slide order:
Sub Thing()
Dim oSl As Slide
For Each oSl In ActivePresentation.Slides
oSl.Name = "SLIDE_" & CStr(oSl.SlideIndex)
Next
End Sub
If you want to get a bit fancier, you can have it pick up the slide's title (if any) as the .Name and use SLIDE_index as the .Name if not:
Sub Thing()
Dim oSl As Slide
Dim sTemp As String
For Each oSl In ActivePresentation.Slides
sTemp = SlideTitle(oSl)
If Len(sTemp) > 0 Then
oSl.Name = oSl.Shapes.Title.TextFrame.TextRange.Text
Else
oSl.Name = "SLIDE_" & CStr(oSl.SlideIndex)
End If
Next
End Sub
Function SlideTitle(oSl As Slide) As String
On Error GoTo ErrHandler
SlideTitle = oSl.Shapes.Title.TextFrame.TextRange.Text
NormalExit:
Exit Function
ErrHandler:
SlideTitle = ""
GoTo NormalExit
End Function
One thing to be careful of here is that PPT may or may not complain if you try to give two slides the same .Name property. In other words, you may want to make sure that your slide titles are unique. There might be other issues (characters that aren't allowed in .Name for example).
Related
I have a PPT presentation with some slides. In each slide I have some superposed images and some buttons (see example image).
I would like to "bring to front" an image when the corresponding button is clicked.
I have been triying with animations, but unfortunately there is no animation to run this action.
After some 'investigation' I think that the only way to achieve that is by a macro.
I have absolutely no experience in VBA programming, but after some search I found out the way to 'bring to front' an image of the active slide by doing:
Sub Bring_front()
Dim sld As Slide
Set sld = Application.ActiveWindow.View.Slide
sld.Shapes("NUCLEI").ZOrder msoBringToFront
End Sub`
Then I insert an action (to execute the macro) to the correspoding button and everything works fine!
The problem is that in my real presentation I have many images and buttons (like 10 for slide)... and I would like to create a macro with if/else statements in order to run an statement if a button is clicked.
I have also been searching how to do that but I have not been able to get it.
Is it possible to do something like that?:
Sub Bring_front()
Dim sld As Slide
Set sld = Application.ActiveWindow.View.Slide
if Greenbutton is clicked Then
sld.Shapes("GREEN_IMAGE").ZOrder msoBringToFront
elseif Redbutton is clicked Then
sld.Shapes("RED_IMAGE").ZOrder msoBringToFront
elseif Bluebutton is clicked Then
sld.Shapes("BLUE_IMAGE").ZOrder msoBringToFront
end
End Sub
Can someone help me with this, please?
Thank you in advance!
Maria
It's actually quite simple:
Add a new module in the VBA editor and paste this into it:
Sub MoveToTop(oSh As Shape)
Call oSh.ZOrder(msoBringToFront)
End Sub
Then assign this MoveToTop macro as an action setting on each of the shapes you want to be able to adjust.
That's it.
Well, almost. If you're on a Mac and find that this doesn't work, it's because some things in the Mac version of PPT are broken. In that case, try this, which should work ok in Windows versions of PPT also:
Sub MoveToTop(oSh as Shape)
Dim oSl as Slide
Dim oShTemp as Shape
' oSh.Parent returns a valid reference to the shape's host slide:
Set oSl = ActivePresentation.Slides(oSh.Parent.SlideIndex)
' and oSh.Name works:
MsgBox oSh.Name
' So we use those two bits to get a reference
' to the clicked shape like so
Set oShTemp = oSl.Shapes(oSh.Name)
Call oShTemp.ZOrder(msoBringToFront)
End Sub
I have been trying to modify a useful piece of code created by David Foster some time ago,
However the one really useful addition to this code is proving hard to find in the references.
I need to get the macro to make sure that the "Slide Image" checkbox is ticked when it applies the notes master to all slides, as some slides have been "frankensteined" into the project.
I am struggling to find any reference to this checkbox in powerpoint references, any ideas?
Sub DReplaceNotesMaster()
' Modified version of code originally posted to
' msnews.microsoft.com public newsgroups by
' David Foster in May of 1999
Dim ctl As CommandBarControl
Dim oSl As Slide
' 700 is the control ID for Layout
Set ctl = CommandBars.FindControl(Id:=700)
ActiveWindow.ViewType = ppViewNotesPage
If (ctl Is Nothing) Then
MsgBox "command not available"
Exit Sub
End If
For Each oSl In ActivePresentation.Slides
' go to the current slide
ActiveWindow.View.GotoSlide (oSl.SlideIndex)
DoEvents
' Bring up the dialog
ctl.Execute
DoEvents
' send it the needed keystrokes
SendKeys "%r{enter}"
DoEvents
Next
End Sub
Oddly, the slide image on a notes page is referred to as a title placeholder. This sub checks every notes page for a placeholder with "Slide Image" in the name and adds it if one is not found. This assumes that someone hasn't used the Selection pane to rename the slide image placeholder. If they have, you'll have to trap the resulting error that displays a message "Invalid request: Slide already contains maximum placeholders of this type".
Sub ShowNotesSlideImage()
Dim oSlide As Slide
Dim oShape As Shape
Dim bTitleFound As Boolean
For Each oSlide In ActivePresentation.Slides
bTitleFound = False
For Each oShape In oSlide.NotesPage.Shapes
If oShape.Type = msoPlaceholder Then
If InStr(oShape.Name, "Slide Image") > 0 Then
bTitleFound = True
End If
End If
Next oShape
If bTitleFound = False Then
oSlide.NotesPage.Shapes.AddPlaceholder Type:=ppPlaceholderTitle
End If
Next oSlide
End Sub
A handy resource for finding control names and ID numbers is Microsoft's Office 2016 Help Files: Office Fluent User Interface Control Identifiers, a free download.
I am trying to reduce the effort needed to keep a certain slide (lets call it SlideXYZ) up to date. SlideXYZ is an important content slide that can be found in multiple slide decks. I initially created slide objects that updated automatically when a change was made in the "source slide". However, slide objects unfortunately don't contain animations (they are simply a snapshot of the actual slide). I am now trying to write a VBA script that will search and replace SlideXYZ in each deck with a newer version of SlideXYZ. However, the slide number is dynamic (it changes when a new slide is added above). I need a static, constant reference to SlideXYZ.
I thought of copying SlideXYZ into all presentations once and then using the Slide.Name property to find all instances of it once an update is needed.
However, it appears that the Slide.Name is reassigned by powerpoint each time the slide is pasted into a new presentation. I need a reference that will not change so that I can find and replace SlideXYZ.
#Asger's suggestion would work but a more consistent approach (IMO) would be to use tags. Any presentation, slide, or shape on a slide can have one or more bits of text attached in the form of a tag.
For example:
ActivePresentation.Slides(1).Tags.Add "SlideIdentifier", "Bob"
will create a tag named SlideIdentifier with a value of Bob on slide #1 in the current presentation. These tags will travel with the slide, wherever it goes.
This page on the PowerPoint FAQ that I maintain has more info on using tags:
http://www.pptfaq.com/FAQ00815_Working_with_Tags_-and_a_bit_about_Functions-.htm
As you already found out: Neither SlideIndex, SlideNumber, SlideID nor Name can be used to identify a copied slide.
You may work with the "alternative text" of a characteristic shape to identify a slide:
Just do a right mouseclick on the shape and edit its alternative text.
Also slide notes may help to identify a slide.
Following prints some slide information to your debug window:
Private Sub IdentifyMySlide()
Dim myslide As PowerPoint.Slide
For Each myslide In ActivePresentation.Slides
Debug.Print "Index: " & myslide.SlideIndex,
Debug.Print "Number: " & myslide.SlideNumber,
Debug.Print "ID: " & myslide.SlideID,
Debug.Print "Name: " & myslide.Name,
If myslide.Shapes.Count > 0 Then
Debug.Print "Alternative ShapeText: " & myslide.Shapes(1).AlternativeText,
End If
If myslide.HasNotesPage Then
If myslide.NotesPage(1).Shapes.Count > 0 Then
If myslide.NotesPage(1).Shapes(1).HasTextFrame Then
Debug.Print "Notes: " & Left(myslide.NotesPage(1).Shapes(1).TextFrame.TextRange.Text, 10)
Else
Debug.Print
End If
Else
Debug.Print
End If
Else
Debug.Print
End If
Next myslide
End Sub
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
I need to make two presentations with the same slides, backgrounds, and everything except for the text: one in German and one in English. Therefore I have two separate presentations which I must always simultaneously update, otherwise one language version will be outdated and I often forget what I changed.
I have made a custom show with all of the slides copied into one PowerPoint presentation and that works fairly well, but I still must change two of the same slides each time that I make an edit to one language.
Therefore, I'm trying to write a macro that will recognize all textboxes within the presentation with German text in them and hide them during the show, and vice versa. Then, each of the macros would be linked to a hyperlinked box on the title slide called 'English' or 'German' which, when clicked, would hide the textboxes in the other languages while also leaving all pictures and formatting the same. Ideally, the macro would hide all boxes in one language and make all the boxes in the other language visible again within the same step, so that the presentation is always usable and I don't have a user who opens a PPT with 'no text boxes' because they would all be hidden...
In order to achieve this I have two text boxes containing the text in both languages on the same slide, that's why I'd like to hide the textboxes.
I am able to get all text boxes hidden but not text boxes in a specific language (aka, all boxes regardless of their editing language will get hidden but not any specific ones).
PS - text boxes do not NEED to be referenced here... it could just refer to a shape. I was trying to avoid, that Pictures would be hidden and thought text boxes would be the best way to reference what I wanted.
Is there a better way?
I don't really know how to reference a language in VBA, I found this website by accident and thought someone might have a quick trick to help me out with this issue. The code I have which will blend out textboxes but not blend out specific language-boxes is as follows:
Sub GermanTextBoxFinder()
Dim SlideToCheck As Slide
Dim ShapeIndex As Integer
' Visit each slide
For Each SlideToCheck In ActivePresentation.Slides
' On each slide, count down through the shapes
For ShapeIndex = SlideToCheck.Shapes.Count To 1 Step -1
' If the shape IS a text box and DOES have German text
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
MsoLanguageID.msoLanguageIDGerman _
Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
End If
Next
Next
End Sub
Why don't you name the shapes with German text something that identifies them? E.g. use the prefix "txtGER" for the texts in German and "txtENG" for the ones in English. Then you could use something like the following:
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
Left(SlideToCheck.Shapes(ShapeIndex).Name, 6) = "txtGER" Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
Else
SlideToCheck.Shapes(ShapeIndex).Visible = msoTrue
End If
(Please see this q+a for information on how to rename the shapes).
This will hide text boxes/shapes with text that contain either English or French. You can modify HideFrench to hide German instead ... Intellisense will provide the correct constants.
Sub HideEnglish()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDEnglishUS Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub
Sub HideFrench()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDFrench Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub