View Full Version : Macro following a slide
YorickG
05-25-2017, 12:29 AM
Hello, is it possible to create a macro that follows specific slide. What I mean. I create a macro for example:
With ActivePresentation.Slides(1).Shapes(3)
it works on slide 1. However when I add another slide as supposed the code will work on new slide 1 but I would like it to work on old slide 1 which is now slide 2. The perfect situation is when vba code recognize where is the slide in presentation in case it mas moved to slide 2, 3 etc.
Thank you very much.
mart.potter
05-27-2017, 04:18 AM
Sub test()
Dim sld As Slide
Set sld = Application.ActiveWindow.View.Slide
With sld.Shapes(3)
.TextFrame.TextRange.Text = "123"
End With
End Sub
Paul_Hossler
05-27-2017, 10:44 AM
Try using SlideID to return the Slide object, instead of the SlideIndex ( e.g. ActivePresentation.Slides(1) )
https://msdn.microsoft.com/en-us/library/office/ff745767.aspx
Unlike the SlideID property, the SlideIndex property of a Slide object can change when you add slides to the presentation or rearrange the slides in the presentation. Therefore, using the FindBySlideID (https://msdn.microsoft.com/en-us/library/office/ff744787.aspx) method with the slide's ID number can be a more reliable way to return a specific Slide object from a Slides collection than using the Item method with the slide's index number.
Simple example -- I created a presentation with 4 slides, titled "1", "2", "3", "4"
The SlideID's are 256, 257, 258, 259
1. Run the macro and the results are "2" and "2" (slide with SlideIndex = 2 has SlideID = 257)
2. Rearrange slides to 1342
3. Run the macro and the results are "3" and "2" (the 'new' slide with SlideIndex = 2 still has SlideID = 258, BUT the original slide 2 still has SlideID = 257)
Option Explicit
Sub Demo()
Dim iSlideID As Long
Dim oSlide As Slide
iSlideID = ActivePresentation.Slides(2).SlideID
Set oSlide = ActivePresentation.Slides.FindBySlideID(iSlideID)
MsgBox oSlide.Shapes(1).TextFrame.TextRange.Text
Set oSlide = ActivePresentation.Slides.FindBySlideID(257)
MsgBox oSlide.Shapes(1).TextFrame.TextRange.Text
End Sub
John Wilson
05-27-2017, 11:16 AM
Slide ID is the way to go but just a comment The SlideID refers to the order the slides were added so you may need to read the slideID first. It will not easily change.
Sub show_ID()Dim osld As Slide
For Each osld In ActivePresentation.Slides
With osld.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 100, 20)
.Name = "ID"
.TextFrame.TextRange = osld.SlideID
End With
Next osld
End Sub
Sub kill_IDAgain()
On Error Resume Next
Dim osld As Slide
For Each osld In ActivePresentation.Slides
osld.Shapes("ID").Delete
Next osld
End Sub
Paul_Hossler
05-27-2017, 03:13 PM
I have a little macro in my PP addin. I don't use SlideID much, but this is handier that writing a throw-away macro
Option Explicit
Sub WhatSlides()
Dim sMessage As String
Dim oSlide As Slide
If ActivePresentation Is Nothing Then Exit Sub
Set oSlide = Application.ActiveWindow.View.Slide
With oSlide
sMessage = "Slide Name = " & .Name & vbCrLf & _
"Slide Number = " & .SlideNumber & " (Displayed Slide #)" & vbCrLf & _
"Slide Index = " & .SlideIndex & " (# in the PPTX)" & vbCrLf & _
"Slide ID = " & .SlideID & " (Order in which added)"
MsgBox sMessage, vbInformation + vbOKOnly, "Information About Current Slide"
End With
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.