Ramyaa Seetharaman
Ramyaa Seetharaman

Reputation: 79

Change shape text in PowerPoint VBA loop

I'm trying to create a timer in PowerPoint. I have written a code to change the text of the shape through VBA loop. In the presentation mode I see only the first and last change. In-between changes are not visible on screen. Is there a way to refresh the object after every change? Please help

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub Test()
    ActivePresentation.Slides(2).Shapes(1).TextFrame.TextRange.Text = 0

    For i = 0 To 5
        Sleep (1000)
        ActivePresentation.Slides(2).Shapes(1).TextFrame.TextRange.Text = i
    Next
End Sub

Upvotes: 1

Views: 10748

Answers (2)

WalterCEden
WalterCEden

Reputation: 11

Just in case it might help someone, I wrote this routine to update the date on every slide in a presentation. Each slide must have the object with the same name on it (mine is simply named "SlideDate") or it will fail. You can hide the object though and the code will still work. Just update the For loop for the number of slides that you have and adjust your object name. It will update hidden slides too.

Sub ChangeFooterDates()

Dim SlideDate As String
    SlideDate = "2/22/21"

    With ActivePresentation
        For n = 1 To 25

            .Slides(n).Shapes("SlideDate").TextFrame.TextRange.Text = SlideDate
        
    Next n                      
End With 'ActivePresentation
    
End Sub

Upvotes: 1

R3uK
R3uK

Reputation: 14537

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub Test()
    With ActivePresentation
        .Slides(2).Shapes(1).TextFrame.TextRange.Text = 0

        For i = 0 To 5
            Sleep (1000)
            .Slides(2).Shapes(1).TextFrame.TextRange.Text = i
            RefreshSlide .SlideShowWindow
        Next i
    End With 'ActivePresentation
End Sub

This routine update the slide on the fly.
If it doesn't work for you, uncomment the line below Adds an empty textbox

Public Sub RefreshSlide(ByVal SlideShowWindowObject As Object)
    With SlideSlideShowWindowObject
        .Height = .Height - 1
        .Height = .Height + 1

        'Adds an empty textbox
        '.View.Slide.Shapes.AddTextbox msoTextOrientationHorizontal, 1, 1, 1, 1

    End With 'SlideSlideShowWindowObject
End Sub

Upvotes: 5

Related Questions