Share via

PowerPoint Macro doesn't work

Donald Ray 60 Reputation points
2026-02-25T20:10:51.02+00:00

The VBA code snippet below is supposed to do the following:

1. Get the current slide.

2. Get a text string  the “TeamScore1” Shape from current slide.

3. Cast the text string as an integer.

4. Store the integer in teamScore1.

Dim sld As Slide

Dim teamScore1 As Integer

Set sld = ActivePresentation.Slides(resultsSlideIndex)

team1Score = CInt(sld.Shapes("Team1Score").TextFrame.TextRange.Text)

It does not work. Please show me what I am doing wrong. Any and all help will be greatly appreciated.

Microsoft 365 and Office | PowerPoint | For home | Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Donald Ray 60 Reputation points
    2026-02-26T13:01:32.4833333+00:00

    Changed:

    team1Score = CInt(sld.Shapes("Team1Score").TextFrame.TextRange.Text)

    This, and it now works

    team1Score = CInt(ActivePresentation.Slides(currentSlideIndex).Shapes("Team1Score").TextFrame.TextRange.Text)

    Even though the following should have worked, but it didn't.

    Set sld = ActivePresentation.Slides(currentSlideIndex)

    team1Score = CInt(sld.Shapes("Team1Score").TextFrame.TextRange.Text)

    0 comments No comments

  2. John Korchok 230.5K Reputation points Volunteer Moderator
    2026-02-25T20:26:31.3033333+00:00

    I don't see any code to set the value of resultsSlideIndex. To get the current active slide use:

    SlideShowWindows(1).View.slide.SlideIndex
    

  3. Q&A Assist
    2026-02-25T20:11:01.9833333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It looks like there might be a couple of issues in your VBA code snippet that could prevent it from working correctly:

    1. Variable Declaration: You declared teamScore1 as Integer, but in your code, you are trying to assign the value to team1Score, which is not declared. Make sure to declare team1Score as an Integer or change the assignment to teamScore1.
    2. Shape Name: Ensure that the shape name "Team1Score" is spelled correctly and matches exactly with the name of the shape in your PowerPoint slide. Shape names are case-sensitive.
    3. Error Handling: Consider adding error handling to catch any issues when accessing the shape or converting the text to an integer. This can help you identify if the shape does not exist or if the text cannot be converted to an integer.

    Here’s a revised version of your code:

    Dim sld As Slide
    Dim teamScore1 As Integer
    
    Set sld = ActivePresentation.Slides(resultsSlideIndex)
    
    If sld.Shapes.Exists("Team1Score") Then
        teamScore1 = CInt(sld.Shapes("Team1Score").TextFrame.TextRange.Text)
    Else
        MsgBox "Shape 'Team1Score' does not exist on the current slide."
    End If
    

    This code checks if the shape exists before trying to access its text.

    Make sure to test the revised code and check for any additional errors that may arise.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.