Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Slide Numbers - PPT

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
So I got really annoyed while trying to figure out how to format slide numbers for Power Point 2007 and decided to write my own macro for it. I want each slide to display <Page Number> of <Total Pages>. I can only get power point to automatically display the current page, but not the total pages. I dont want to have manually change the total page number for every slide every time I add or delete a slide. So here is my function:

Code:
Option Explicit
Function TotalSlides()
    Dim objPresentation As Presentation
    Set objPresentation = Application.Presentations(1)
    TotalSlides = objPresentation.Slides.Count
End Function

So now my question is: How do I get power point to call the function and return the value into a textbox? I tried
= <#> & "/" & TotalSlides()
But it's not working.


-JTBorton
Well, You can try banging your head against the wall, but you just end up with lost-time injuries and damaged equipment. [M. Passman]
 
Please post the full code of trying to get any text into the textbox. How are you executing it?

Why are you putting the brackets at the end?

In a 13 slide presentation, the following:
Code:
Option Explicit
Function TotalSlides() As Long
Dim objPresentation As Presentation
Set objPresentation = Application.Presentations(1)
TotalSlides = objPresentation.Slides.Count
End Function

Sub BlahOfYadda()
Dim oSlide As Slide
Dim oShape As Shape
Dim j As Long

For Each oSlide In ActivePresentation.Slides
   j = oSlide.SlideIndex
   Set oShape = oSlide.Shapes _
      .AddTextbox(msoTextOrientationHorizontal, 100, 490, 323, 28)
   oShape.TextFrame _
         .TextRange.Text = j & " of " & TotalSlides
Next
End Sub
puts a textbox with "1 of 13", "2 of 13", "3 of 13" etc. etc. in all the slides. Hopefully you can take it from there.

Gerry
 
Actually, for efficiency sake....
Code:
Function TotalSlides() As Long
Dim objPresentation As Presentation
Set objPresentation = Application.Presentations(1)
TotalSlides = objPresentation.Slides.Count
End Function


Sub BlahOfYadda()
Dim oSlide As Slide
Dim oShape As Shape
Dim j As Long
Dim ThisTotalCount As Long
ThisTotalCount = TotalSlides

For Each oSlide In ActivePresentation.Slides
   j = oSlide.SlideIndex
   Set oShape = oSlide.Shapes _
      .AddTextbox(msoTextOrientationHorizontal, 100, 490, 323, 28)
   oShape.TextFrame _
         .TextRange.Text = j & " of " & ThisTotalCount
Next
End Sub
That way, the Function is not called for each iteration/slide. Better once, than 13 times, yes?

Gerry
 
Umm, well actually the code I posted was my entire code. I was hoping I could just call the function from a slide, like I can from a spreadsheet. So I have to do with a subroutine? That makes power point kind of lame.

-JTBorton
Well, You can try banging your head against the wall, but you just end up with lost-time injuries and damaged equipment. [M. Passman]
 
That makes power point kind of lame. "

No, it does not. Why do you think that?

" I was hoping I could just call the function from a slide, like I can from a spreadsheet. So I have to do with a subroutine? "

a) - you can call a procedure from a slide.
b) - how were you thinking of calling it without a sub?

"Umm, well actually the code I posted was my entire code. "

But your entire code does not DO anything really. It just gets a number (.Count).

The code is easily adjusted to action the current slide.

Gerry
 
I agree with Fumei. I thought the same thing when reading the 'lame' comment. I don't see why anyone would expect that they can type a formula into a textbox the way they can in a spreadsheet and expect tht it is supposed to work.

Powerpoint is not Excel. For that matter, it is not Photoshop, Flash, or Lightwave either. It does have it's shortcomings and quirks, but because it doesn't work like a spreadsheet isn't necessarily one of them.
 
Code:
Sub ThisSlide()
Dim j As Long
j = ActiveWindow.Selection.SlideRange.SlideIndex
ActiveWindow.Selection.SlideRange _
   .Shapes _
      .AddTextbox(msoTextOrientationHorizontal, 48, 474, 232, 28).Select
ActiveWindow.Selection.TextRange.Text = j & " of " & TotalSlides
End Sub
will put a textbox as "2 of 14", or "23 of 46", or "4 of 6"...or whatever.


You STILL have to have some method to call the procedure. This could be a keyboard shortcut; a icon put on a toolbar; or a menu item - any of the normal methods for firing a procedure. In the case of Powerpoint, you can also have an ActveX control fire the code, although this only works while the slide show is running. Or, you can use a "hot spot" on the slide.

But you still have to use some method to fire the procedure.

Were you thinking you could make a textbox and just type in "TotalSlides"? A textbox is text, it does not execute anything.

I do not know (which is why I asked) what you were doing with:
I tried = <#> & "/" & TotalSlides()

Tried where? Tried how? WHAT equals that? What does <#> mean?

And, again, why use the brackets - TotalSlides(),instead of TotalSlides?

Gerry
 
In any case JT, a Powerpoint textbox does not have formula capabilities. It is not Excel. Just like putting:

= yadda & TotalSlides

into a Word table cell does absolutely nothing. For example, if you have the following in a code module:
Code:
Function YaddaBlah() As Long
   YaddaBlah = 123
End Function
You can put "YaddaBlah" anywhere you like in a Word document, and it does....NOTHING. Neither can you insert it as a Function by any Formula (in a table cell).

Why? Because Word is not a spreadsheet, nor is Powerpoint.

In fact, I am nore it would work even in Excel. Mind you, I know diddely about Excel. But I tried to put:

= YaddaBlah

into a cell (after writing the Function to a code module), and it errors out. So I do not even know how you use a UDF in a cell formula...although I would assume it is in fact possible.

But it is NOT possible in Powerpoint. Yes, you can use a UDF, but it has to be called by something.

Gerry
 
Thanks for all the information guys. I've mostly only worked with Excel and assumed that I could simply calla function from a slide as you would a cell in a worksheet.

fumei
Tried where? Tried how? WHAT equals that? What does <#> mean?

And, again, why use the brackets - TotalSlides(),instead of TotalSlides?

the <#> is a key symbol to insert the number of the current slide. Power point itself added this, I simply amended it. The & are the concatenation operators and TotalSlides() is the name of my function.

In fact, I am nore it would work even in Excel. Mind you, I know diddely about Excel. But I tried to put:

= YaddaBlah

into a cell (after writing the Function to a code module), and it errors out. So I do not even know how you use a UDF in a cell formula...although I would assume it is in fact possible.

Don't put a space after the '=' sign, and you still need to pass empty parameters. Try "=YaddaBlah()"

Thanks again guys! You are always a great help. Now I know a little more about programming in other Microsoft environments.

-JTBorton
Well, You can try banging your head against the wall, but you just end up with lost-time injuries and damaged equipment. [M. Passman]
 
Don't put a space after the '=' sign, and you still need to pass empty parameters. Try "=YaddaBlah()"

I have:
Code:
Function yaddablah() As Long
yaddablah = 123
End Function

I put =yaddablah() - no space - into a cell.

It errors out.

Gerry
 
hmmm.. I tried your function and it worked fine for me. So the next items on the checklist are:

1) Did you enable macros for Excel?
2) Did you place the function in a new standard module instead of a worksheet class module or the ThisWorkBook class module? Location is key.

-JTBorton
Well, You can try banging your head against the wall, but you just end up with lost-time injuries and damaged equipment. [M. Passman]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top