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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting PwerPoint slides as jpg into a folder using vba Access 1

Status
Not open for further replies.

aldi07

MIS
Jun 22, 2010
100
CA
Hi,
I am using Access vba to export all slides from a PowerPoint presentation, as jpg files, into a folder.
It does not work, because I am obviously making many syntax mistakes:

Code:
Private Sub pptTest()
    Dim objPresentation As PowerPoint.Presentation
    Dim objSlide As PowerPoint.Slide
    Dim strFileName As String
    Dim j As Integer
    Dim x As Integer
    Dim mobjPPT As PowerPoint.Application

    Set mobjPPT = New PowerPoint.Application
    
    Set objPresentation = mobjPPT.Presentations.Open("C:\MyPowerPointPathAndName.pptm")
    
    j = objPresentation.Slides.Count
    If j > 0 Then
        With objPresentation

                For x = 1 To j
                    ActivePresentation.Slides(x).Export _
                    FileName:="MyFileName" & "str(x),jpg"
                Next x
                pptPres.Slides.Range.Delete

        End With
    End If
    
End Sub

I need guidance here. Thank you.
 
One line to correct:
[pre] objPresentation.Slides(x).Export _
FileName:="MyFileName" & Format(x, String(Len(Cstr(j)),"0")) & ".jpg", FilterName:="JPG"[/pre]

[tt]Format[/tt] instead of raw x for better sorting if j>9. A path is missing in FileName, you can specify to avoid default.


combo
 
It worked combo. Thank you very much.
Actually you made me realise also that I was using ActivePresentation instead of objPresentation.
Another mistake: I was using pptPres instead of objPresentation.
Once all the correction were made, it worked fine. Thank you again!
 
It may be beneficial to other TT visitors to know your full code solution.
And how about a Star for combo?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Andrzejek,
I did give combo a star, except that I forgot to confirm it... [surprise] Well, it's done now!
Here is the code as requested:
Code:
Private Sub PPT_Test_Click()
    Dim objPresentation As PowerPoint.Presentation
    Dim objSlide As PowerPoint.Slide
    Dim strFileName As String
    Dim i As Long
    Dim j As Long
    Dim x As Integer
    Dim mobjPPT As PowerPoint.Application

    Set mobjPPT = New PowerPoint.Application
    
    Set objPresentation = mobjPPT.Presentations.Open("MyPathAndPowerPointFileName.pptm")
    
    j = objPresentation.Slides.Count
    If j > 0 Then
        With objPresentation
            For x = 1 To j
                objPresentation.Slides(x).Export _
                    FileName:="MyPathAndSlideFileName" & Format(x, String(Len(CStr(j)), "0")) & ".jpg", FilterName:="JPG"
            Next x
            objPresentation.Slides.Range.Delete
        End With
    End If
    MsgBox "Done"

End Sub
 
Not to be picky, but your [tt]With objPresentation / End With[/tt] is either not needed or not used.

Either:
Code:
...
    If j > 0 Then
        With objPresentation
            For x = 1 To j
                [s]objPresentation[/s].Slides(x).Export _
                    FileName:="MyPathAndSlideFileName" & Format(x, String(Len(CStr(j)), "0")) & ".jpg", FilterName:="JPG"
            Next x
            [s]objPresentation[/s].Slides.Range.Delete
        End With
    End If
...
or
Code:
...
    If j > 0 Then
        [s]With objPresentation[/s]
            For x = 1 To j
                objPresentation.Slides(x).Export _
                    FileName:="MyPathAndSlideFileName" & Format(x, String(Len(CStr(j)), "0")) & ".jpg", FilterName:="JPG"
            Next x
            objPresentation.Slides.Range.Delete
        [s]End With[/s]
    End If
...
but not both [wiggle]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
You are quite right Andy.
I did not understand the exact way "with / end with" should be used.
It is clearer now. Thank you. By the way, I chose your first solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top