Consulting

Results 1 to 5 of 5

Thread: Macro following a slide

  1. #1
    VBAX Regular
    Joined
    May 2017
    Posts
    18
    Location

    Macro following a slide

    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.

  2. #2
    VBAX Regular mart.potter's Avatar
    Joined
    Jul 2014
    Location
    Tallinn, Estonia (EU)
    Posts
    9
    Location
    Sub test()
        Dim sld As Slide
        Set sld = Application.ActiveWindow.View.Slide
        With sld.Shapes(3)
            .TextFrame.TextRange.Text = "123"
        End With
    End Sub
    Last edited by mart.potter; 05-27-2017 at 04:45 AM.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Try using SlideID to return the Slide object, instead of the SlideIndex ( e.g. ActivePresentation.Slides(1) )

    https://msdn.microsoft.com/en-us/lib.../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 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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •