hunter21188
hunter21188

Reputation: 415

VBA Loop Within a Loop Where the Second Loop Always Starts at 1

I have the following code below that loops through all slides in a PPT presentation. If the slides fall under one of the sections and are not hidden, it will save them with an appropriate name. I am trying to get the number after the saved name (i.e. TEST#) to always start over at 1 for each section. So if there are five slides within the IDSS section, which starts at slide 5, then they would be saved: IDSS1, IDSS2, etc. The way I have it below just saves it with the current slide number. Thanks for any help you can provide!

Edit to Provide More Clearity

I have many different slides in this PPT presentation, all of which are located within a particular named section. For this example, assume there are 6 total slides. Slides 1-3 are in the section named "TEST" and slide 3 is hidden. Slides 4-6 are in the section "IDSS" and none are hidden. I need the code to loop through all the slides, figure out which ones are in the "TEST" section and name them "TEST1" and "TEST2" (the third slide is skipped since it is hidden). Then it moves on to the section "IDSS" and saves the slides as "IDSS1", IDSS2", and "IDSS3". It's the number after the name I can't get to restart at 1 for each section. Hope this makes it more clear.

Dim sld As Slide

TestSection = SectionIndexOf("Test") 'Name of a section is in the quotes.

IDSSslides = SectionIndexOf("IDSS")

i = 1
For Each sld In ActivePresentation.Slides

    If sld.sectionIndex = TestSection And ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoFalse Then
        ActivePresentation.Slides(i).Export filenamepng & "TEST" & i & ".png", "PNG"
    ElseIf sld.sectionIndex = IDSSslides And ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoFalse Then
        ActivePresentation.Slides(i).Export filenamepng & "IDSS" & i & ".png", "PNG"
    End If
i = i + 1
Next

Upvotes: 0

Views: 61

Answers (1)

JustGreat
JustGreat

Reputation: 561

Your question is not really clear, but I think that this is what you need, try it and let me know

    Dim sld As Slide

TestSection = SectionIndexOf("Test") 'Name of a section is in the quotes.

IDSSslides = SectionIndexOf("IDSS")

dim i as integer 'Counter for TEST 
dim j as integer'Counter for IDSS 

i = 1 
j=1

For Each sld In ActivePresentation.Slides

    If sld.sectionIndex = TestSection And sld.SlideShowTransition.Hidden = msoFalse Then
        sld.Export filenamepng & "TEST" & i & ".png", "PNG"
        i=i+1
    ElseIf sld.sectionIndex = IDSSslides And sld.SlideShowTransition.Hidden = msoFalse Then
        sld.Export filenamepng & "IDSS" & j & ".png", "PNG"
        j=j+1
    End If
Next

Upvotes: 1

Related Questions