count number of slides?

  • Thread starter Thread starter Geoff Cox
  • Start date Start date
G

Geoff Cox

Hello,

I have hundreds of presenations and would like to know the total
number of slides - can anyone point me at macro code for this?

Thanks

Geoff
 
On Mon, 12 Jun 2006 09:12:52 +0100, Geoff Cox

I am trying the following code - it is the sub MyMacro which I need a
little help with.

I can get the number of slides for each presentation but how do I get
the total number of slides for all presentations?

I guessing I need something like

slidesPerPresentation = ActivePresentation.Slides.Count

totalSlides = totalSlides + slidesPerPresentaion

but not clear how to do this.

Thanks

Geoff




Sub check_each_slide_for_buttons()

Dim rayFileList() As String
Dim FolderPath As String
Dim FileSpec
Dim strTemp As String
Dim x As Long

FolderPath = "c:\test\activities\" ' Note: MUST end in \
FileSpec = "*.ppt"

ReDim rayFileList(1 To 1) As String
strTemp = Dir$(FolderPath & FileSpec)
While strTemp <> ""
rayFileList(UBound(rayFileList)) = FolderPath & strTemp
ReDim Preserve rayFileList(1 To UBound(rayFileList) + 1) As
String
strTemp = Dir
Wend

If UBound(rayFileList) > 1 Then
For x = 1 To UBound(rayFileList) - 1
Call MyMacro(rayFileList(x))

Next x
End If

End Sub

Sub MyMacro(strMyFile As String)

Dim oPresentation As Presentation
Set oPresentation = Presentations.Open(strMyFile)

With oPresentation

MsgBox = "number of slides = " & ActivePresentation.Slides.Count


oPresentation.Close


End With




End Sub
 
Geoff

You need to declare a variable that has global scope (read up on Scope
in Help)

Put at the top of the module before any Sub routines the following
Public lSlideTotal as Long

Then modify MyMacro to add the counter
With oPresentation

MsgBox = "number of slides = " & ActivePresentation.Slides.Count


oPresentation.Close
'Next line adds the running total. Starts at 0 on first pressie
lSlideCount = lSlideCount + activepresentation.slides.count


Brian Reilly, MVP
 
Try rewriting MyMacro as a function instead of a sub:
I'm also renaming it to make its use more obvious

Function CountSlides(strMyFile As String) as Long
' Open a presentation named in strMyFile
' Return the number of slides in the presentation

Dim oPresentation As Presentation

Set oPresentation = Presentations.Open(strMyFile)

CountSlides = oPresentation.Slides.Count

oPresentation.Close

End Sub

Now in your main code you can use something like:

' assuming you Dim lTotalSlides as Long:
lTotalSlides = lTotalSlides + CountSlides(strMyFile)
 
Geoff

You need to declare a variable that has global scope (read up on Scope
in Help)

Put at the top of the module before any Sub routines the following
Public lSlideTotal as Long

Then modify MyMacro to add the counter

'Next line adds the running total. Starts at 0 on first pressie
lSlideCount = lSlideCount + activepresentation.slides.count

Brian,

I seme to have used your suggests correctly in that the code works but
if I run it successively the total keeps adding to itself - where do I
prevent this?

Thanks

Geoff


Public lSlideCount As Long

Sub count_slides()

Dim rayFileList() As String
Dim FolderPath As String
Dim FileSpec
Dim strTemp As String
Dim x As Long


FolderPath = "c:\test\activities\" ' Note: MUST end in \
FileSpec = "*.ppt"


' Fill the array with files that meet the spec above
ReDim rayFileList(1 To 1) As String
strTemp = Dir$(FolderPath & FileSpec)
While strTemp <> ""
rayFileList(UBound(rayFileList)) = FolderPath & strTemp
ReDim Preserve rayFileList(1 To UBound(rayFileList) + 1) As
String
strTemp = Dir
Wend

If UBound(rayFileList) > 1 Then
For x = 1 To UBound(rayFileList) - 1
Call MyMacro(rayFileList(x))

Next x
End If

MsgBox "total = " & lSlideCount

End Sub

Sub MyMacro(strMyFile As String)


Dim oPresentation As Presentation
Set oPresentation = Presentations.Open(strMyFile)


With oPresentation

lSlideCount = lSlideCount + ActivePresentation.Slides.Count


oPresentation.Close


End With




End Sub
 
Try rewriting MyMacro as a function instead of a sub:
I'm also renaming it to make its use more obvious

Function CountSlides(strMyFile As String) as Long
' Open a presentation named in strMyFile
' Return the number of slides in the presentation

Dim oPresentation As Presentation

Set oPresentation = Presentations.Open(strMyFile)

CountSlides = oPresentation.Slides.Count

oPresentation.Close

End Sub

Now in your main code you can use something like:

' assuming you Dim lTotalSlides as Long:
lTotalSlides = lTotalSlides + CountSlides(strMyFile)


Steve,

I have not used a function before with VBA - how does it work? Do I
call a function as with a sub or is this different?

Cheers

Geoff
 
Functions are just like Subs that return values. That's why the function
has a type (the As Long at the end of the first line). Call it just like
a Sub, but then do something with the result that is returned. The result
that is returned is whatever the name of the Function is set to in the
function (in this case, that is the line: CountSlides = ...)
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

 
Steve,

I have not used a function before with VBA - how does it work? Do I
call a function as with a sub or is this different?

What David said. But you call it as per my example:

' assuming you Dim lTotalSlides as Long:
lTotalSlides = lTotalSlides + CountSlides(strMyFile)
 
Functions are just like Subs that return values. That's why the function
has a type (the As Long at the end of the first line). Call it just like
a Sub, but then do something with the result that is returned. The result
that is returned is whatever the name of the Function is set to in the
function (in this case, that is the line: CountSlides = ...)
--David


Thanks David.

Cheers

Geoff
 
What David said. But you call it as per my example:

' assuming you Dim lTotalSlides as Long:
lTotalSlides = lTotalSlides + CountSlides(strMyFile)

Steve,

OK - I see.

Thanks

Geoff
 
Geoff,
Steve and David's ideas work as well.

If using my approach, you'd want to throw in a msgbox at the very end
of MyMacro to tell you the number of total slides and then clear the
value of the lSlidesCount by either setting it to nothing or to 0.

Brian Reilly, MVP
 
Geoff,
Steve and David's ideas work as well.

If using my approach, you'd want to throw in a msgbox at the very end
of MyMacro to tell you the number of total slides and then clear the
value of the lSlidesCount by either setting it to nothing or to 0.

Thanks Brian,

Cheers

Geoff
 
Back
Top