Solved

Counting slides in powerpoint that meet a specified condition using vba

Posted on 2014-01-20
3
1,539 Views
Last Modified: 2014-01-27
I have a series of slides which I want to be able to count if they meet a specific condition (and perform other function on ideally). I tried doing this by

activepresentation.slides.shapes.title.name("test").count

but this doesn't work. I then tried to set this up as an array but I can't work out how to do that. I tried the following, but it's not working, as I think I can't define an array this way. I feel like this should be easy but I can't work it out.

Sub arraytest()

Dim ppt As Presentation
Dim s As Slide
Dim scurrent As Slide
Dim arraytest As Variant

Set ppt = ActivePresentation
Set s = ppt.Slides(ActiveWindow.Selection.SlideRange.SlideIndex)

arraytest = Array(ppt.Slides(ActiveWindow.Selection.SlideRange.SlideIndex))

For x = 1 To ppt.Slides.Count Step 1
    If ppt.Slides(x).Shapes.Title.Name = "test" Then
        ActiveWindow.View.GotoSlide (x)
        Set scurrent = ppt.Slides(ActiveWindow.Selection.SlideRange.SlideIndex)
    End If

    ReDim Preserve arraytest(UBound(arraytest) + 1)
    arraytest = ppt.Slides(ActiveWindow.Selection.SlideRange.SlideIndex)

Next

MsgBox ppt.Slides.Range(arraytest).Count

End Sub
0
Comment
Question by:jessicasimonk
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39794340
I think that you have several misconceptions.
This code creates an array of the slide indexes where the Title shape has text = "test"
Sub arraytest()

Dim ppt As Presentation
Dim s As Slide
Dim scurrent As Slide
Dim arraytest() As Integer
Dim x As Integer
Dim p As Integer

Set ppt = ActivePresentation

For x = 1 To ppt.Slides.Count Step 1
    If ppt.Slides(x).Shapes.Title.TextFrame.TextRange = "test" Then
        ReDim Preserve arraytest(p)
        arraytest(p) = x
        p = p + 1
   End If
Next x

MsgBox p & " slides found"

End Sub

Open in new window

0
 

Author Comment

by:jessicasimonk
ID: 39794555
Thank you - I knew I had it completely wrong but I'm a VBA newbie and couldn't work it out
0
 
LVL 23

Expert Comment

by:JSRWilson
ID: 39798480
Be careful not to have any slide with no title (eg blank) because it will crash.

Also it is probably easier without an array

Sub No_arraytest()
Dim osld As Slide
Dim raySlides() As Long
Dim iCount As Integer
Dim strReport As String
For Each osld In ActivePresentation.Slides
If osld.Shapes.HasTitle Then
If osld.Shapes.Title.TextFrame.TextRange = "test" Then
iCount = iCount + 1
strReport = strReport & "Slide " & osld.SlideIndex & ","
End If
End If
Next osld
Select Case iCount
Case Is > 1
MsgBox iCount & " Slides were found:" & vbCrLf & Left(strReport, Len(strReport) - 1)
Case 1
MsgBox iCount & " Slides was found:" & vbCrLf & Left(strReport, Len(strReport) - 1)
Case 0
MsgBox "None found."
End Select
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This video teaches viewers how to add simple and professional themes to their slides.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now