1

I am using the following VBA code to export PPT slides to images. The problem is that, for Slides 1 through 9, the filenames have only one character (eg: "1.png"). I need for those first nine slides to have TWO characters in their filename (eg: "01.png") to match the later slides (eg: "10.png").

How can I make that happen?

Thanks in advance!

Sub Save_PowerPoint_Slide_as_Images()

Dim sImagePath As String
Dim sImageName As String
Dim oSlide As Slide '* Slide Object
Dim lScaleWidth As Long '* Scale Width
Dim lScaleHeight As Long '* Scale Height
On Error GoTo Err_ImageSave

For Each oSlide In ActivePresentation.Slides
sImageName = oSlide.SlideNumber & ".png"
oSlide.Export sImagePath & sImageName, "PNG"
Next oSlide

Err_ImageSave:
If Err <> 0 Then
MsgBox Err.Description
End If
End Sub

Function sImagePath() As String
sImagePath = ActivePresentaion.Path
End Function
brettdj
  • 54,857
  • 16
  • 114
  • 177
Tim S
  • 209
  • 1
  • 2
  • 7

1 Answers1

3

try using Format ie

sImageName = Format(oSlide.SlideNumber, "00") & ".png"
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • Thanks for the very quick close out :) – brettdj Nov 07 '11 at 02:10
  • 2
    One minor quibble: you probably want to use Slide.Index rather than Slide.Number. Slide.Number gives you the number that will print when you use slide number footers, and the user can change the starting number in page setup. The first slide might have a Slide.Number of 0 or any other random thing. If you use Slide.Index, the first slide will ALWAYS be 1, the second will be 2 and so on. – Steve Rindsberg Nov 07 '11 at 15:59
  • That is a good point Steve, my code was focused on the formatting issue. Regard Dave (+1 on the comment) – brettdj Nov 08 '11 at 02:13
  • @brettdj Just to make sure, then: that line should read: sImageName = Format(oSlide.SlideIndex, "00") & ".png" Also, what if I need to export the filename as three digits? Is it: sImageName = Format(oSlide.SlideIndex, "000") & ".png" ...that doesn't seem to work right - I still get "01.png" instead of "001.png" THanks! – Tim S Nov 10 '11 at 14:14